Python 使用数据帧作为查找
我有两个数据帧,这一个:Python 使用数据帧作为查找,python,pandas,data-analysis,Python,Pandas,Data Analysis,我有两个数据帧,这一个: 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 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 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-type函数的示例,但我似乎无法使语法正常工作。有人能帮我吗
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
您可以只执行lhs.mergerhs,on='item',how='left,这将在item上匹配,并添加相应的值,在没有匹配的情况下,NaN将显示为完美。非常感谢。
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