[英]How to find the maximum value of a column/row combination in a concatenated dataframe in Pandas
After concatenating four multindexed tables with yearly kg/ha data I end up with a dataframe containing 22617 rows and 144 columns.在将四个多索引表与年度 kg/ha 数据连接起来后,我最终得到一个包含 22617 行和 144 列的 dataframe。 What I want to do is to find the maximum of each index/year combination to have a dataframe with 36 columns.我想要做的是找到每个索引/年份组合的最大值,以拥有一个具有 36 列的 dataframe。 Here is an example of the data with two columns of two of the initial dataframes:这是具有两个初始数据帧的两列的数据示例:
Y1980 Y1981 Y1980 Y1981
FID_CATCHM CCA_2 GRIDCODE
0 1059.0 2 21.70426 22.058224 21.70426 22.058224
3 21.70426 22.058224 0.00000 0.000000
4 0.00000 0.000000 21.70426 22.058224
1 1059.0 2 0.00000 0.000000 21.70426 22.058224
4 21.70426 22.058224 21.70426 22.058224
2 1001.0 2 20.71299 21.058432 20.71299 21.058432
3 0.00000 0.000000 20.71299 21.058432
1054.0 2 20.25414 20.283833 20.25414 20.283833
4 0.00000 0.000000 20.25414 20.283833
1059.0 2 21.70426 22.058224 21.70426 22.058224
3 21.70426 22.058224 21.70426 22.058224
4 21.70426 22.058224 21.70426 22.058224
3 1059.0 1 21.70426 22.058224 0.00000 0.000000
2 21.70426 22.058224 21.70426 22.058224
3 21.70426 22.058224 21.70426 22.058224
4 21.70426 22.058224 21.70426 22.058224
4 1058.0 1 0.00000 0.000000 23.79386 24.201496
2 23.79386 24.201496 23.79386 24.201496
3 0.00000 0.000000 0.00000 0.000000
4 23.79386 24.201496 23.79386 24.201496
What I tried to do is to use a mask我试图做的是使用面具
df_max = (df
.groupby(['FID_CATCHM',
'CCA_2', 'GRIDCODE'])
.max())
df_mask = df_max.max(axis=1).to_frame('maximum')
but the output is identical to the concatenated dataframe.但 output 与串联的 dataframe 相同。 How can this be done?如何才能做到这一点? I appreciate every help.我感谢每一个帮助。
I think you need max
per columns and if necessary then per MultiIndex:我认为您需要每列的max
,如果需要,则需要每个 MultiIndex:
df = df.max(level=0, axis=1).max(level=[0,1,2], axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.