Python Forum

Full Version: append dataframes in loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have a code with two dataframes types which are collected by simulation. I want to append these dataframes and store them into separated csv. When I print the dataframes within the loop, it was fine, but when I print the aggregated dataframe outside the loop, it is empty.

df1 = pd.DataFrame()
df2 = pd.DataFrame()
for i in range(n):
    df_temp1 = get_df1(i)
    df1.append(df_temp1, ignore_index=True)

    df_temp2 = get_df2(i)
    df2.append(df_temp2,ignote_index=True)

df1.to_csv('f1.csv')
df2.to_csv('f2.csv')
Any hint?

Thanks
df1.append returns a new object So, change lines 5-8 to
    df1 = df1.append(df_temp1, ignore_index = True)
    df_temp2 = get_df2(i)
    df2 = df2.append(df_temp2,ignore_index=True)
and it will work fine.