Python-Pandas:给定行中最小值的数字/索引 [英] Python - Pandas: number/index of the minimum value in the given row

查看:255
本文介绍了Python-Pandas:给定行中最小值的数字/索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,其中包含一行和多列.

I have one pandas dataframe, with one row and multiple columns.

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

I want to get the column number/index of the minimum value in the given row.

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

The code I found was: df.columns.get_loc('colname')

以上代码要求输入列名.我的数据框没有列名.我想获取最小值的列位置.

The above code asks for a column name. My dataframe doesn't have column names. I want to get the column location of the minimum value.

推荐答案

使用

Use argmin with converting DataFrame to array by values, only necessary only numeric data:

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

这篇关于Python-Pandas:给定行中最小值的数字/索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆