Python Pandas -根据其他列值选择列



示例数据帧:

<表类> column_name b c d tbody><<tr>5317b32110d453314

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

相关内容

  • 没有找到相关文章

最新更新