Python - Pandas:给定行中最小值的数字/索引

Rid*_*mar 2 python data-manipulation pandas

我有一个 pandas 数据框,具有一行和多列。

我想获取给定行中最小值的列号/索引。

我找到的代码是: df.columns.get_loc('colname')

上面的代码要求提供列名称。我的数据框没有列名。我想获取最小值的列位置。

jez*_*ael 5

argminDataFrame通过 转换为数组一起使用values,仅需要数字数据:

df = pd.DataFrame({
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[-5,3,6,9,2,-4]

})

print (df)
   B  C  D  E
0  4  7  1 -5
1  5  8  3  3
2  4  9  5  6
3  5  4  7  9
4  5  2  1  2
5  4  3  0 -4

df['col'] = df.values.argmin(axis=1)
print (df)
   B  C  D  E  col
0  4  7  1 -5    3
1  5  8  3  3    2
2  4  9  5  6    0
3  5  4  7  9    1
4  5  2  1  2    2
5  4  3  0 -4    3
Run Code Online (Sandbox Code Playgroud)