我有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
和只包含上述项目子集的查找表:
item Price
Apples 1.99
Oranges 6.99
当第一个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类型函数,但我似乎无法使语法工作。有人可以帮助我吗?
答案 0 :(得分:1)
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