【发布时间】:2021-12-13 03:47:54
【问题描述】:
我正在尝试将两个多索引数据框放在一起。原始数据框具有完全相同的列和相似的值,然后按相同的列分组。
如下所示的原始数据框示例(我正在制作列,因为实际数据是机密的)
| receiver | product_name | sent_time | receive_time | product_count | subcomponent_count_1 | subcomponent_count_2 | packer |
|---|---|---|---|---|---|---|---|
| John LLC | Apple watch | 2021-10-20 | 2021-10-21 | 20 | 10 | 15 | employee1 |
| Sam LLC | Apple pencil | 2021-10-05 | 2021-10-06 | 10 | 7 | 2 | employee1 |
| .. | .. | .. | .. | .. | .. | .. | .. |
我转换数据框的步骤:
df3 = df1.groupby(['receiver','product_name','sent_time','receive_time','product_count']...
df4 = df2.groupby(['receiver','product_name','sent_time','receive_time','product_count']...
df_i_want = pd.concat([df_3,df_4])
但是,df_i_want 没有多索引结构,而是将所有 groupby 列放在同一列中。
如何将这两个多索引 df 放在一起(连接 df1 和 df2 然后进行 groupby 不是一个选项)?
谢谢大家!
【问题讨论】:
-
groupby的目的是什么?如果您只是想拥有一个包含所有行的df1和df2的数据框,您可以这样做df_i_want = df1.append(df2)?
标签: python pandas dataframe multi-index