【发布时间】:2015-11-28 09:22:27
【问题描述】:
我在 pandas python 中有两个数据框:
df1:
Fruit Origin
0 Apple Spain
1 Apple France
2 Apple Italy
3 Banana Germany
4 Banana Portugal
5 Grapes France
6 Grapes Spain
df2:
Fruit
0 Apple
1 Banana
2 Grapes
我想通过 df2 中每个水果的索引来修改 df1 中的 Fruit 列,我要查找的结果应该如下所示:
df1:
Fruit Origin
0 0 Spain
1 0 France
2 0 Italy
3 1 Germany
4 1 Portugal
5 2 France
6 2 Spain
我尝试过的方法是:
df1['Fruit'] = df1.Fruit.apply(lambda x: df2.index[df2.Fruit == x])
但是我正在处理一个大数据集,因此需要花费太多时间,我正在寻找一个更快的选项来执行此操作。
【问题讨论】: