![](/img/trans.png)
[英]How do I find the indices of the row and the column for the maximum value in a Pandas dataframe?
[英]How to find the maximum value of a column/row combination in a concatenated dataframe in Pandas
在將四個多索引表與年度 kg/ha 數據連接起來后,我最終得到一個包含 22617 行和 144 列的 dataframe。 我想要做的是找到每個索引/年份組合的最大值,以擁有一個具有 36 列的 dataframe。 這是具有兩個初始數據幀的兩列的數據示例:
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
我試圖做的是使用面具
df_max = (df
.groupby(['FID_CATCHM',
'CCA_2', 'GRIDCODE'])
.max())
df_mask = df_max.max(axis=1).to_frame('maximum')
但 output 與串聯的 dataframe 相同。 如何才能做到這一點? 我感謝每一個幫助。
我認為您需要每列的max
,如果需要,則需要每個 MultiIndex:
df = df.max(level=0, axis=1).max(level=[0,1,2], axis=0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.