簡體   English   中英

在 pandas DataFrame 中選擇特定值

[英]Selecting specific values in a 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 中,我有五個不同實驗( a04d, a04e, a04f, a04g, a04h 0, 1, 2, 3 pbias, pcorr, pcorr_anom, kge

我想 select 在每個實驗中得分pbias > 100pcorr <= 0.2的站。

考慮示例 DataFrame,唯一應該退出的站應該是sta = 2 ,因為對於所有exp a pbias > 100pcorr <= 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

我不知道如何進行,任何指示都會非常有用! 謝謝!

為每條記錄創建一個標志,這樣您就可以輕松地使用groupby-all來確定組中的所有標志是否都為真(在您的情況下為sta )。 隨后,可以使用df.isin()選擇具有合格sta值的行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM