Pithit
Pithit

Reputation: 471

store for loop results in one dataframe

I am trying to store all the dataframes generated by this code in just one dataframe. This is my code:

df_big = pd.DataFrame()
for i in range(1,3):
  df = pd.read_csv('https://s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2016-0' + str(i) + '.csv')
  df_big.append(df)

print(df_big.shape)

However, my result is an empty DF. Any help would be appreciated.

Upvotes: 0

Views: 438

Answers (1)

Henru
Henru

Reputation: 133

Appending your data to an empty dataframe will give you another empty dataframe.

Try using pd.concat:

import pandas as pd

df_big = pd.DataFrame()
df = pd.DataFrame(['a','b','c'])

df_big = pd.concat([df_big,df])

print(df_big)
print("Shape of df_big: " + str(df_big.shape))

Output:

enter image description here

Upvotes: 1

Related Questions