【发布时间】:2023-04-08 05:52:01
【问题描述】:
我有两个带有示例数据的大型 CSV 文件,如下所示:
df1 =
Index Fruit Vegetable
0 Mango Spinach
1 Berry Carrot
2 Banana Cabbage
df2 =
Index Unit Price
0 Mango 30
1 Artichoke 45
2 Banana 12
3 Berry 10
4 Cabbage 25
5 Rice 40
6 Spinach 34
7 Carrot 08
8 Lentil 12
9 Pot 32
我想创建以下数据框:
df3 =
Index Fruit Price Vegetable Price
0 Mango 30 Spinach 34
1 Berry 10 Carrot 08
2 Banana 12 Cabbage 25
我希望在 df1 中逐行比较每个单位的价格。如果价格在 5 美元以内,我想将它们输出到单独的数据框中,如下所示:
df4 =
Index Fruit Price Vegetable Price
0 Mango 30 Spinach 34
1 Berry 10 Carrot 08
实现这一目标的通用方法是什么?提前谢谢你。
【问题讨论】:
-
我建议在这里使用两个joins 将来自 df2 的价格信息与 df1 结合起来。
标签: python pandas dataframe merge