Reputation: 2478
I have a Python 3 dictionary of Pandas DataFrame, which has the structure as follows for each DataFrame-
time animal_id x y Distance Average_Speed Average_Acceleration Direction
0 1.0 312.0 405.29 417.76 0.000000 0.000000 0.0 0.000000
5 2.0 312.0 405.31 417.37 0.070000 0.082164 0.0 -90.000000
10 3.0 312.0 405.31 417.07 0.215407 0.157562 0.0 -68.198591
15 4.0 312.0 405.30 416.86 0.192354 0.171618 0.0 -62.102729
20 5.0 312.0 405.29 416.71 0.162788 0.182343 0.0 -47.489553
The dimension/shape of each of the five Pandas DataFrame is-
for animal_id in data_animal_id_groups.keys():
data_animal_id_groups[animal_id].reset_index(drop = True)
Animal ID = 312, Shape = (43201, 4)
Animal ID = 511, Shape = (43201, 4)
Animal ID = 607, Shape = (43201, 4)
Animal ID = 811, Shape = (43201, 4)
Animal ID = 905, Shape = (43201, 4)
I want to merge all the 5 or in general n Pandas DataFrame contained in the dictionary object into one CSV file. How should I proceed?
Please note that the structure of each Pandas DataFrame will remain the same. However, the number of rows may vary.
Thanks!
Upvotes: 4
Views: 7816
Reputation: 323266
With dict
you can using concat
pd.concat(data_animal_id_groups.values()) #.to_csv('youfile.csv')
Upvotes: 4
Reputation: 1101
This should do it:
df = pd.DataFrame() # Create an empty dataframe, this will be your final dataframe
for key, sub_df in data_animal_id_groups.items():
df = df.append(sub_df, ignore_index=False) # Add your sub_df one by one
print(df)
And if you want to save it as a csv:
df.to_csv("merged_dataframe.csv")
Upvotes: 2