Python 熊猫,从一列中选择最大值,从另一列中选择最小值
我有一个这样的数据帧Python 熊猫,从一列中选择最大值,从另一列中选择最小值,python,pandas,numpy,Python,Pandas,Numpy,我有一个这样的数据帧 A B 25 0.5 21 0.6 17 0.7 14 0.7 <--- this is the row I want 12 0.3 ab 25 0.5 21 0.6 17 0.7 14 0.7IIUC,使用where+idxmin: df.iloc[df.where(df.B.eq(df.B.max())).A.idxmin()]
A B
25 0.5
21 0.6
17 0.7
14 0.7 <--- this is the row I want
12 0.3
ab
25 0.5
21 0.6
17 0.7
14 0.7IIUC,使用where
+idxmin
:
df.iloc[df.where(df.B.eq(df.B.max())).A.idxmin()]
A 14.0
B 0.7
Name: 3, dtype: float64
首先比较列B
bymax
值,然后获取最小A
by的索引,最后选择by:
详细信息:
print (df.loc[df['B'] == df['B'].max(), 'A'])
2 17
3 14
Name: A, dtype: int64
可能的副本通常我同意,但我不认为副本涵盖it@jpp不知道,考虑到这是一个非常简单的问题,这个网站的某个地方可能有一个复制品,但事实并非如此one@filippo-别担心,如果问题结束了,让我或cᴏʟᴅsᴘᴇᴇᴅ “知道”和“问题”将重新打开。;)
print (df.loc[df['B'] == df['B'].max(), 'A'])
2 17
3 14
Name: A, dtype: int64