Reputation: 531
I have a pandas DataFrame as follow:
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
In this DataFrame I have scores (pbias, pcorr, pcorr_anom, kge
) of four different stations (0, 1, 2, 3
) for five different experiments (a04d, a04e, a04f, a04g, a04h
).
I would like to select the station for which in each experiments the score pbias > 100
or pcorr <= 0.2
.
Considering the example DataFrame the only station that should be out should be sta = 2
since it is the only one you has for all exp
a pbias > 100
or 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
I have no idea about how to proceed, any indication would be really usefull! Thanks!
Upvotes: 0
Views: 46
Reputation: 4648
Create a flag for each record, so you can use groupby-all
easily to determine if all flags are true in groups (sta
in your case). Subsequently, rows with qualified sta
values can be selected using 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)]
output
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
Upvotes: 1