1

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)

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Do you have any idea of way new_df.append(df1) keeps new_df empy?
I think because append doesnt work inplace - returning a new object
I thought it was as in a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.