【发布时间】:2023-02-23 02:53:48
【问题描述】:
假设我有数据框 A 和 B,索引为time,列表列为food。这两个数据框都类似于历史日志,即我当时拥有的水果和蔬菜:
A:
food
time
2021-08-20 ["apple","orange"]
2021-08-28 ["apple","orange","banana"]
乙:
food
time
2021-08-19 ["squash"]
2021-08-24 ["squash","carrot"]
2021-08-29 ["carrot"]
我怎样才能结合这两个数据框,以便它同时跟踪水果和蔬菜?
food
time
2021-08-19 ["squash"]
2021-08-20 ["apple","orange","squash"]
2021-08-24 ["apple","orange","squash","carrot"]
2021-08-28 ["apple","orange","banana","squash","carrot"]
2021-08-29 ["apple","orange","banana","carrot"]
本质上,我想合并行,并且对于每一行,合并该时间戳之前两个最新条目的食物。保证 A 和 B 中的食物项不重叠,并且 A 和 B 之间的时间戳不重叠。
我尝试直接使用 pd.concat([A,B]) ,但它不会组合食物。
【问题讨论】:
标签: python pandas concatenation