我正在尝试从df5分配正确的FinalZone到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。我很迷失。

最佳答案

我认为您需要在这里lookup

#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/

10-12 18:11