我想使用excel的vlookup等效的新数据帧。我有2个数据框,我正在尝试针对df1.Column A value
对df2.Column A and B
进行v-lookup Value A
并获取df1.Column A
。
旁边的单元格df2.Column A and B
值value B
并获得Current ouput
Data frame 1 Dataframe2
AC1 AC2 AC10 AC20
Bus 5 car 1
car 3 helicopter 7
Walking 2 running 5
。
数据看起来像 -
对于数据帧1和2,数据分别在A列和B列中
Dataframe [Neu]
NaNa NaNa
Car 1
NaNa NaNa
期望/预期输出
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')
我试过了:
df8 = df1['AC3'] = df1.AC1.map(df2.AC10)
print (df8)
df8 = df1['AC4'] = df1.AC1.map(df2.AC20)
print (df8)
我取得了一些成功:
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)
确切的输出是NaN,所以它不正确。
示例:
UISlider
答案 0 :(得分:1)
您可以查看以下使用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)
它将输出以下内容:
AC1 AC20
0 Bus NaN
1 car 1.0
2 Walking NaN