python trics

pandas.DataFrame 最值索引

2018-11-20  本文已影响0人  要干啥_懵_

工作需要,查询某一列中的最大值所对应的索引。强大的pandas早就为我们写好了封装函数: idxmax()、idxmin()
举个栗子:
先随便生成一个数据结构test

test = {'year':[2016,2016,2017,2017,2017,2018,2018],
        'num':[2,5,4,7,8,90,78],
        'name':['a','b','c','d','e','f','g']}
test = pd.DataFrame(test)

输出结果:

 name   num     year
0   a   2   2016
1   b   5   2016
2   c   4   2017
3   d   7   2017
4   e   8   2017
5   f   90  2018
6   g   78  2018

调用函数dataframe.idxmax(axis=0,skipna=True), 参数axis指定寻找最值的方向,按照行的方式或者列的方式

test[['num','year']].idxmax(axis=0)

输出:

num     6
year    5
dtype: int64

哒哒~!就找到了以列为方式的最大的num的索引为6,year的索引为5

上一篇下一篇

猜你喜欢

热点阅读