使用 Pandas DataFrame 作为查找

Using a Pandas DataFrame as Lookup

我有 2 个 pandas DataFrame,这个:

item    inStock     description  
Apples    10        a juicy treat
Oranges   34        mediocre at best
Bananas   21        can be used as phone prop
<...many other fruits...>
Kiwi       0        too fuzzy

和仅包含上述项目子集的查找 table:

item     Price
Apples   1.99
Oranges  6.99

我想浏览第一个 table 并在第一个 DataFrame 中的水果与第二个 DataFrame 中的水果匹配时为 DataFrame 填写价格列:

item    inStock     description                   Price
Apples    10        a juicy treat                 1.99
Oranges   34        mediocre at best              6.99
Bananas   21        can be used as phone prop
<...many other fruits...>
Kiwi       0        too fuzzy

我查看了带有内置查找函数以及使用 where-in 类型函数的示例,但我似乎无法使语法正常工作。有人可以帮我吗?

import pandas as pd

df_item= pd.read_csv('Item.txt')
df_price= pd.read_csv('Price.txt')

df_final=pd.merge(df_item,df_price ,on='item',how='left' )
print df_final

输出

      item  inStock                description  Price
0   Apples       10              a juicy treat   1.99
1  Oranges       34           mediocre at best   6.99
2  Bananas       21  can be used as phone prop    NaN
3     Kiwi        0                  too fuzzy    NaN