为了账号安全,请及时绑定邮箱和手机立即绑定

在 pandas DataFrame 中选择特定值

在 pandas DataFrame 中选择特定值

哆啦的时光机 2024-01-04 17:17:57
我有一个 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我不知道如何继续,任何指示都会非常有用!谢谢!
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

为每个记录创建一个标志,以便您可以groupby-all轻松地确定组中的所有标志是否都为 true(sta在您的情况下)。随后,sta可以使用 选择具有限定值的行df.isin()。


df["flag"] = (df["pbias"] > 100) & (df["pcorr"] < 0.2) 

sr_sta = df.groupby("sta")["flag"].all()


# qualified sta's

sta_yes = sr_sta.index.values[sr_sta]


ans = df[df["sta"].isin(sta_yes)]

输出


print(ans)

    sta   exp      pbias   pcorr  pcorr_anom      kge  flag

2     2  a04d  3857.0583  0.1138      0.1138 -51.5705  True

6     2  a04e   504.9175 -0.0676     -0.0676  -6.0067  True

10    2  a04f   596.7894  0.0608      0.0608  -5.7271  True

14    2  a04g   440.5116  0.0694      0.0694  -3.8980  True

18    2  a04h   393.3018  0.0318      0.0318  -3.0665  True


查看完整回答
反对 回复 2024-01-04
  • 1 回答
  • 0 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信