[英]vlookup in pandas between 2 dataframes to create third dataframe
I am wanting to use the equivalent to excel's vlookup for a new dataframe. 我想将等效于excel的vlookup用于新的数据框。 I have 2 dataframes and am trying to v-lookup df1.Column A value
against df2.Column A and B
and getting Value A
. 我有2个数据帧,并尝试对df2.Column A and B
进行v-lookup df1.Column A value
并获取Value A
And the cell beside that is df1.Column A
value against df2.Column A and B
and getting value B
. 旁边的单元格是df1.Column A
相对df2.Column A and B
value B
df2.Column A and B
并获得value B
Data looks like- 数据看起来像-
The data is in Columns A and B respectively for both data frames 1 and 2 数据分别位于数据帧1和2的列A和B中
Current ouput
Data frame 1 Dataframe2
AC1 AC2 AC10 AC20
Bus 5 car 1
car 3 helicopter 7
Walking 2 running 5
Desired/Expected output 期望/预期输出
Dataframe [Neu]
NaNa NaNa
Car 1
NaNa NaNa
I have tried: 我努力了:
dfz = df1.insert(2, '2A2', df1['AC1'].map(df2.set_index('AC1')['2A2']))
print (dfz)
result = left.join(right, on=['AC2', 'AC1], how='inner')
#left.join(right, lsuffix='_l', rsuffix='_r')
#df1.join(df1.set_index('AC2')['AC1'], on='AC2')
I have had some success with: 我在以下方面取得了一些成功:
df8 = df1['AC3'] = df1.AC1.map(df2.AC10)
print (df8)
df8 = df1['AC4'] = df1.AC1.map(df2.AC20)
print (df8)
The exact output is NaN so it's not correct. 确切的输出是NaN,所以它是不正确的。
Example: 例:
df1 = pd.read_excel('C:/Users/Desktop/zav.xlsx')
df2 = pd.read_excel('C:/Users/Desktop/zav2.xlsx')
#df3 = pd.merge(df, df2)
df3 = df1.join(df2)
print (df3)
todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date-datetime.timedelta(10), periods=10, freq='D')
df5 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
columns=['a', 'b', 'c', 'd', 'e'])
print(df5)
df8 = df1['AC3'] = df1.AC1.map(df2.AC10)
print (df8)
df8 = df1['AC3'] = df1.AC1.map(df2.AC20)
print (df8)
You can check the following code working with map
: 您可以检查以下使用map
代码:
import pandas as pd
df1 = pd.DataFrame([["Bus",5],["car",3],["Walking",2]],columns=["AC1","AC2"])
df2 = pd.DataFrame([["car",1],["helicopter",7],["running", 5]],columns=["AC10","AC20"])
df2 = df2.groupby("AC10").first()
df3= df1.join(df2,on="AC1",how="left").drop("AC2",axis=1)
It will output the following: 它将输出以下内容:
AC1 AC20
0 Bus NaN
1 car 1.0
2 Walking NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.