簡體   English   中英

如何 select pandas 行在一個列中具有最大值,來自一組共享兩個公共列的行?

[英]How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

下面的 Pandas DataFrame df有 5 列,彩色,而索引號在最左邊的黑色。

在此處輸入圖像描述

請注意最后兩列(我們稱它們為col4col5 )具有 static 編號,表示數據的段、組或塊。 其他組(在這兩列中更改其 static 編號)已從屏幕截圖中隱藏。

如何挑出第三列(稱為col3 )中具有最大值的行或行的索引,用黑色圈出: 1.90977 ,條件是最后兩行是 static? 換句話說,挑出組中最好的行

尋找這樣的東西,這是行不通的:

df.loc[(df['col3'] == 0.999141) & (df['col4'] == 0.000861559)]

If not last 2 columns has same values use numpy.isclose for select columns by some precision, also for performance is better select by DataFrame.loc by mask and column name:

df.loc[np.isclose(df['col4'], 0.999141) & np.isclose(df['col5'], 0.000861559), 'col3'].max()

對於最大值使用Series.idxmax的索引:

df.loc[np.isclose(df['col4'], 0.999141) & np.isclose(df['col5'], 0.000861559), 'col3'].idxmax()

對於 select 通過最大col4和最小col5使用:

df.loc[df['col4'].eq(df['col4'].max()) & df['col5'].eq(df['col5'].min()), 'col3'].max()

df.loc[df['col4'].eq(df['col4'].max()) & df['col5'].eq(df['col5'].min()), 'col3'].idxmax()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM