获取数据帧中最大值的(row,col)索引 [英] Get (row,col) indices of max value in dataframe
本文介绍了获取数据帧中最大值的(row,col)索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据框看起来像这样。
I have a data frame that looks something like this.
import pandas as pd
data = [[5, 7, 10], [7, 20, 4,], [8, 1, 6,]]
cities = ['Boston', 'Phoenix', 'New York']
df = pd.DataFrame(data, columns=cities, index=cities)
输出:
Boston Phoenix New York
Boston 5 7 10
Phoenix 7 20 4
New York 8 1 6
我希望能够找到价值最高的城市对。在这种情况下,我想返回菲尼克斯凤凰城。
And I want to be able to find the city pair with the greatest value. In this case I would want to return Phoenix,Phoenix.
我试过:
cityMax = df.values.max()
cityPairs = df.idxmax()
第一个给我最大值(20),第二个给我每个城市最大对不仅仅是总体最大值。有没有办法在数据框中返回指定值的索引和列标题?
The first one only gives me the largest value (20) and the second gives me each cities max pair not just the overall max. Is there a way to return the index and column header for a specified value in a dataframe?
推荐答案
使用unstack()并解压缩顶部MultiIndex作为元组使用idxmax()
Use unstack() and extract the top MultiIndex as a tuple using idxmax()
import pandas as pd
data = [[5, 7, 10], [7, 20, 4,], [8, 1, 6,]]
cities = ['Boston', 'Phoenix', 'New York']
df = pd.DataFrame(data, columns=cities, index=cities)
print df.unstack().idxmax()
返回:
('Phoenix', 'Phoenix')
这篇关于获取数据帧中最大值的(row,col)索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文