Kiran
Kiran

Reputation: 45

Problem with appending values into a empty dataframe

frame = pd.DataFrame()
for i in range (1995,2020):
  file_name = f"{i}"
  df = pd.read_csv(BytesIO(uploaded["%s.csv"%file_name]))
  df = pd.DataFrame(data, columns= ['DATE','ARANGALI'])
  frame.append(df)
print(frame)

I tried to define a function to append all the the data I have into one dataframe. The output appears to be an empty dataframe.

Empty DataFrame; Columns: []; Index: [].

The code works if consider the variables frame and df as lists and appends up to a large list. But I want a dataframe with all the data under the same column heads. What am I doing wrong?

Upvotes: 0

Views: 121

Answers (1)

Alex
Alex

Reputation: 741

Append method returns a new DataFrame. Docs.

frame = pd.DataFrame()
for i in range (1995,2020):
  file_name = f"{i}"
  df = pd.read_csv(BytesIO(uploaded["%s.csv"%file_name]))
  df = pd.DataFrame(data, columns= ['DATE','ARANGALI'])
  frame = frame.append(df)
print(frame)

Upvotes: 1

Related Questions