Append data frames to and empty variable

I've several data frames which all have the same columns:

df_list = [df1,df2,df3,dfn]

And I want to append those to a new data frame if a given condition is satisfied.

for df in df_list:
     if some_condition:
           new_df.append(df)

But that didn;t work, so I try this:

new_df = pd.DataFrame()
for column in df1.columns:
    new_df[column] = []

And just for testing it:

new_df.append(df1)

but new_df it's still empty.

It only works if I assign it explicity

new_df = new_df.append(df1)

Upvotes: 1

Views: 103

Answers (1)

jezrael
jezrael

Reputation: 863501

I think you need append to new list dfs and then concat:

dfs = []
for df in df_list:
     if some_condition:
           dfs.append(df)

print pd.concat(dfs)

Upvotes: 2

Related Questions