我正在尝试将正确的 FinalZone 从 df5 分配到 df7。
df7:
OrigZone DestZone FinalZone
5 6 (output should be 9)
8 5 (output should be 2)
df5(FinalZone 查找表):
Zip3 1 2 3 4 5 6
5 7 2 3 5 8 9
6 3 1 5 8 8 8
7 3 3 8 8 9 1
8 5 5 6 6 2 2
def zone_assign(row):
return df5.iloc[df5.loc[df5['ZIP3']==row['OrigZone']].index,row['DestZone']]
df7.apply(zone_assign, axis=1)
当我运行这个时,我得到了 df5 的打印输出,其中有一堆 NaN
。我很失落。
最佳答案
我认为你需要在这里查找
#df5=(df5.set_index('Zip3'))
#df5.columns=df5.columns.astype(int)
df7['FinalZone']=df5.lookup(df7.OrigZone,df7.DestZone)
df7
Out[130]:
OrigZone DestZone FinalZone
0 5 6 9
1 8 5 2
关于python - 在 python 中使用 iloc 和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49758183/