Python Forum
append dataframes in loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append dataframes in loop
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging two DataFrames based on indexes from two other DataFrames lucinda_rigeitti 0 1,725 Jan-16-2020, 08:36 PM
Last Post: lucinda_rigeitti

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020