用函数在python中使用iloc

2024-10-02 10:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从df5到df7分配正确的FinalZone。你知道吗

东风7:

 OrigZone    DestZone    FinalZone
 5           6           (output should be 9)
 8           5           (output should be 2)   

df5(最终区域查找表):

  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。我太迷路了。你知道吗


Tags: 区域zoneoutputdefberowassignshould
2条回答

这是一种方法,假设您的df5列是整数。你知道吗

df7['FinalZone'] = df7.apply(lambda x: df5.loc[x['OrigZone'], x['DestZone']], axis=1)

print(df7)

#    OrigZone  DestZone  FinalZone
# 0         5         6          9
# 1         8         5          2

我想你需要这里

#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

相关问题 更多 >

    热门问题