简体   繁体   English

根据列的最大值删除pandas数据帧行

[英]Drop pandas dataframe row based on max value of a column

I have a Dataframe like so: 我有一个像这样的数据帧:

      p_rel      y_BET  sq_resid
1  0.069370  41.184996  0.292942
2  0.116405  43.101090  0.010953
3  0.173409  44.727748  0.036832
4  0.225629  46.681293  0.540616
5  0.250682  46.980616  0.128191
6  0.294650  47.446113  0.132367
7  0.322530  48.078038  0.235047

How do I get rid of the fourth row because it has the max value of sq_resid? 如何摆脱第四行,因为它的最大值为sq_resid? note: the max will change from dataset to dataset so just removing the 4th row isn't enough. 注意:最大值将从数据集更改为数据集,因此仅删除第4行是不够的。

I have tried several things such as I can remove the max value which leaves the dataframe like below but haven't been able to remove the whole row. 我已经尝试了几样的东西,比如我可以删除最大值,这样就像下面那样留下了数据帧但是却无法删除整行。

  p_rel      y_BET  sq_resid
1  0.069370  41.184996  0.292942
2  0.116405  43.101090  0.010953
3  0.173409  44.727748  0.036832
4  0.225629  46.681293  Nan
5  0.250682  46.980616  0.128191
6  0.294650  47.446113  0.132367
7  0.322530  48.078038  0.235047

You could just filter the df like so: 您可以像这样过滤df:

In [255]:
df.loc[df['sq_resid']!=df['sq_resid'].max()]

Out[255]:
      p_rel      y_BET  sq_resid
1  0.069370  41.184996  0.292942
2  0.116405  43.101090  0.010953
3  0.173409  44.727748  0.036832
5  0.250682  46.980616  0.128191
6  0.294650  47.446113  0.132367

or drop using idxmax which will return the label row of the max value: drop使用idxmax这将返回最大值的标签行:

In [257]:
df.drop(df['sq_resid'].idxmax())

Out[257]:
      p_rel      y_BET  sq_resid
1  0.069370  41.184996  0.292942
2  0.116405  43.101090  0.010953
3  0.173409  44.727748  0.036832
5  0.250682  46.980616  0.128191
6  0.294650  47.446113  0.132367
7  0.322530  48.078038  0.235047

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM