【发布时间】:2018-10-18 01:11:24
【问题描述】:
我正在尝试在多索引 Pandas 数据框中查找多列中最大值的索引。
Kommune Upplands Vallentuna... Kiruna
Year Party
1973 M 0.9 29.2 ... 20
KD 15 10 ... 2
MP 1.1 4 ... 5
V 6 7 ... 8
SD NaN NaN ... NaN
L 10.1 13.5 ... 8.8
1976 M 1.8 29.2 ... 20
KD 16 10 ... 2
MP 10 4 ... 5
V 15 7 ... 8
SD NaN NaN ... NaN
L 11.9 15 ... 18
... ... ... ... ... ...
... ... ... ... ... ...
2014 M 28 22 ... 29
KD 4.5 13 ... 5
MP 11 8 ... 9
V 1.9 5 ... 10
SD 20 10 ... 5
L 19 25 ... 1
想要的输出是
Kommune Upplands Vallentuna... Kiruna
Year
1973 KD M ... M
1976 V M ... M
... ... ... ... ...
2014 M L ... M
我尝试过使用groupby(正如之前关于多索引的帖子中所建议的那样-Getting max values from pandas multiindex dataframe),但它会为每个位置返回一个元组。
Kommune Upplands Vallentuna ... Kiruna
Year
1973 (1973, KD) (1973, M) ... (1973, M)
1976 (1976, V) (1976, M) ... (1976, M)
... ... ... ... ...
2014 (2014, M) (2014, L) ... (2014, M)
如何仅从每个元组中获取第二个元素?或者有没有更有效的方法来查找索引?
【问题讨论】:
标签: python pandas multi-index