在 pandas DataFrame 中选择特定值

我有一个 pandas DataFrame 如下:


  sta   exp      pbias   pcorr  pcorr_anom      kge

   0  a04d  1256.8834  0.1384      0.1384 -18.8759

   1  a04d  1052.9256  0.1625      0.1625 -11.4252

   2  a04d  3857.0583  0.1138      0.1138 -51.5705

   3  a04d  2683.4755  0.2693      0.2693 -31.2720

   0  a04e   898.1652 -0.0196     -0.0196  -9.5759

   1  a04e  1645.8625  0.0903      0.0903 -18.3872

   2  a04e   504.9175 -0.0676     -0.0676  -6.0067

   3  a04e  725.4790 -0.0063     -0.0063  -9.3833

   0  a04f   724.0266  0.0955      0.0955  -9.9355

   1  a04f  1612.8359 -0.0917     -0.0917 -23.1014

   2  a04f   596.7894  0.0608      0.0608  -5.7271

   3  a04f  2910.2085  0.1413      0.1413 -31.9109

   0  a04g   271.3087 -0.0511     -0.0511  -3.5811

   1  a04g  1584.6974  0.1106      0.1106 -21.5528

   2  a04g   440.5116  0.0694      0.0694  -3.8980

   3  a04g   -19.5232 -0.1285     -0.1285  -0.2710

   0  a04h    48.2395 -0.0960     -0.0960  -0.9461

   1  a04h   -40.6854 -0.1344     -0.1344  -0.2702

   2  a04h   393.3018  0.0318      0.0318  -3.0665

   3  a04h    86.1273 -0.1313     -0.1313  -0.4778

在此 DataFrame 中,我有五个不同实验pbias, pcorr, pcorr_anom, kge( ) 的四个不同站 ( ) 的分数 ( )。0, 1, 2, 3a04d, a04e, a04f, a04g, a04h


我想选择在每个实验中得分pbias > 100或 的电台pcorr <= 0.2。


考虑到示例 DataFrame ,唯一应该退出的站点应该是,sta = 2因为它是所有expapbias > 100或的唯一站点pcorr <= 0.2。


  sta   exp      pbias   pcorr  pcorr_anom      kge

   2  a04d  3857.0583  0.1138      0.1138 -51.5705

   2  a04e   504.9175 -0.0676     -0.0676  -6.0067

   2  a04f   596.7894  0.0608      0.0608  -5.7271

   2  a04g   440.5116  0.0694      0.0694  -3.8980

   2  a04h   393.3018  0.0318      0.0318  -3.0665

我不知道如何继续,任何指示都会非常有用!谢谢!


哆啦的时光机
浏览 58回答 1
1回答

一只萌萌小番薯

为每个记录创建一个标志,以便您可以groupby-all轻松地确定组中的所有标志是否都为 true(sta在您的情况下)。随后,sta可以使用 选择具有限定值的行df.isin()。df["flag"] = (df["pbias"] > 100) & (df["pcorr"] < 0.2)&nbsp;sr_sta = df.groupby("sta")["flag"].all()# qualified sta'ssta_yes = sr_sta.index.values[sr_sta]ans = df[df["sta"].isin(sta_yes)]输出print(ans)&nbsp; &nbsp; sta&nbsp; &nbsp;exp&nbsp; &nbsp; &nbsp; pbias&nbsp; &nbsp;pcorr&nbsp; pcorr_anom&nbsp; &nbsp; &nbsp; kge&nbsp; flag2&nbsp; &nbsp; &nbsp;2&nbsp; a04d&nbsp; 3857.0583&nbsp; 0.1138&nbsp; &nbsp; &nbsp; 0.1138 -51.5705&nbsp; True6&nbsp; &nbsp; &nbsp;2&nbsp; a04e&nbsp; &nbsp;504.9175 -0.0676&nbsp; &nbsp; &nbsp;-0.0676&nbsp; -6.0067&nbsp; True10&nbsp; &nbsp; 2&nbsp; a04f&nbsp; &nbsp;596.7894&nbsp; 0.0608&nbsp; &nbsp; &nbsp; 0.0608&nbsp; -5.7271&nbsp; True14&nbsp; &nbsp; 2&nbsp; a04g&nbsp; &nbsp;440.5116&nbsp; 0.0694&nbsp; &nbsp; &nbsp; 0.0694&nbsp; -3.8980&nbsp; True18&nbsp; &nbsp; 2&nbsp; a04h&nbsp; &nbsp;393.3018&nbsp; 0.0318&nbsp; &nbsp; &nbsp; 0.0318&nbsp; -3.0665&nbsp; True
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python