合并Pandas字典DataFram

2024-07-05 10:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python3dictionary of Pandas DataFrame,它对每个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

五只熊猫数据帧的尺寸/形状为-

^{pr2}$

我想将dictionary对象中包含的所有5个或一般n个Pandas数据帧合并到一个CSV文件中。我该怎么做?在

请注意,每个Pandas数据帧的结构将保持不变。但是,行数可能会有所不同。在

谢谢!在


Tags: of数据iddataframepandastime尺寸结构
2条回答

使用dict可以使用concat

pd.concat(data_animal_id_groups.values()) #.to_csv('youfile.csv')

应该这样做:

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)

如果您想将其保存为csv:

^{pr2}$

相关问题 更多 >