我试图在多索引数据帧中找到多列中最大值的索引。你知道吗
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)
如何从每个元组中仅获取第二个元素?或者有没有更有效的方法来找到指数?你知道吗
看来你需要
一种解决方案是通过^{} 提取每个
tuple
的第二个元素:或使用功能替代方案:
相关问题 更多 >
编程相关推荐