定位列或者行的最大值所在位置。
有三种方法(argmax,仍然存在,idxmax和numpy.argmax)
# pandas:
ind = df["A"].idxmax()
row = df.iloc[ind,:]
# numpy:
#将array转化为DataFrame
arr=pd.DataFrame(array,columns=["one","two","three","four"])
#分别求行最大值及最大值所在索引
arr['max_value']=arr.max(axis=1)
arr['max_index']=np.argmax(array,axis=1)
# ex1:
text = pd.DataFrame([[21,45,78],[23,56,89],[14,25,36],[47,58,69]]) # 先建立一个矩阵
print(text)
Out[1]:
0 1 2
0 21 45 78
1 23 56 89
2 14 25 36
3 47 58 69
# 找到最大值和最大值所对应的位置
text.stack().max()
Out[2]: 89
text.stack().idxmax()
Out[3]: (1, 2)
# 或者是p,q = text.stack().idxmax() # 将值赋给p,q
# 找最小值和最小值所对应的位置
text.stack().min()
Out[4]: 14
text.stack().idxmin()
# 获得某行的最大值对应的的列索引(包含多个相等的最大值的情况)
import pandas as pd
import numpy as np
# create a dataframe
table = pd.DataFrame(np.zeros([3,2]), index=["s1", "s2", "s3"], columns=["a1","a2"])
"""
a1 a2
s1 0.0 0.0
s2 0.0 0.0
s3 0.0 0.0
"""
# 获得 s1 行
s1 = table.loc["s1",:]
# 获得 s1 中最大值的索引,可能有多个
s1_argmax = s1[s1 == s1.max()].index
# randomly choose 1 index
s1_argmax = np.random.choice(s1_argmax)
# 报错的情况,TypeError: reduction operation 'argmin' not allowed for this dtype,
# 解决办法,类型转换:列的数据转为float64.
# 把df的每列数据类型打印出来看看。
print df.dtypes
df['low']=df['low'].astype('float64')