[英]Slicing pandas multiindex dataframe using max of second level
Supposing that I have this MultiIndex dataframe called df
:假设我有这个名为df
的 MultiIndex dataframe :
| |Value
Year |Month|
1992 | 1 | 3
| 2 | 5
| 3 | 8
-----------------
1993 | 1 | 2
| 2 | 7
----------------
1994 | 1 | 20
| 2 | 50
| 3 | 10
| 4 | 5
How do I select all years and max month for each of those years?我如何 select 所有年份和这些年份的最大月份?
I'd like the following result:我想要以下结果:
| |Value
Year |Month|
1992 | 3 | 8
-----------------
1993 | 2 | 7
----------------
1994 | 4 | 5
I've tried to use我试过用
df.loc[(slice(None), [3, 2, 4]),:]
This works, but it's hard-coded.这有效,但它是硬编码的。 How do I set it to bring always the maximum month level instead of saying it manually?如何将其设置为始终带来最大月份级别,而不是手动说出来?
My index are sorted, so it would be take the last month for each year.我的索引已排序,因此每年需要最后一个月。
I've also tried to use the .iloc
but it doesn't work with multiindex我也尝试过使用.iloc
但它不适用于 multiindex
>>> df.iloc[(slice(None), -1),:]
...
IndexingError: Too many indexers
...
you can group on the first level and take the last of the second level and then df.loc[]
:您可以在第一级分组并采取第二级的最后一个然后df.loc[]
:
df.loc[pd.DataFrame.from_records(df.index).groupby(0)[1].last().items()]
Value
Year Month
1992 3 8
1993 2 7
1994 4 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.