示例数据帧:
<表类>
column_name
b
c
d
tbody><<tr>5 3 1 7 b3 2 1 10 d4 5 33 14 表类>
Pandas的文档中有这样一节查找数据:link:
# Get positions and unique values for `column_name`:
idx, cols = pd.factorize(df.column_name)
#Select the columns via a reindex:
result = df.reindex(cols, axis = 1)
# dump down into numpy to select the values:
result = result.to_numpy()[np.arange(len(df)), idx]
df.assign(result = result)
Out[203]:
column_name a b c d result
0 a 5 3 1 7 5
1 b 3 2 1 10 2
2 d 4 5 33 14 14