2 回答

TA貢獻1820條經(jīng)驗 獲得超2個贊
使用pd.DataFrame.lookup
整數(shù)并將其映射到列標(biāo)簽:
df['new'] = df.lookup(df.index, 'x' + df['colum'].astype(str))
print(df)
x1 x2 x3 x4 x5 colum new
0 206 214 21 122 554 2 214
1 226 234 123 456 789 4 456
2 245 253 558 855 123 5 123
3 265 272 0 111 222 4 111
4 283 291 214 589 996 1 283

TA貢獻1812條經(jīng)驗 獲得超5個贊
使用numpy indexing:
df['new'] = df.values[np.arange(len(df)), df['colum'] - 1]
print (df)
x1 x2 x3 x4 x5 colum new
0 206 214 21 122 554 2 214
1 226 234 123 456 789 4 456
2 245 253 558 855 123 5 123
3 265 272 0 111 222 4 111
4 283 291 214 589 996 1 283
添加回答
舉報