簡體   English   中英

多個條件-在pandas數據框中選擇行

[英]Multiple conditions - Selecting rows in pandas dataframe

我有x和y坐標為:

x = (16764.83, 16752.74, 16743.1)
y = (107347.67, 107360.32, 107362.96)

它基本上就像三個點(x1, y1), (x2, y2) and (x3, y3)

在數據框中:

print (bf)
     XMORIG    YMORIG  ZMORIG        XC         YC      ZC
0  14212.37  104364.2    1300  16774.83  107357.67  2852.5
1  14212.37  104364.2    1300  17499.87  105601.70  2867.5
2  14212.37  104364.2    1300  17474.87  105601.70  2867.5
3  14212.37  104364.2    1300  17499.87  105626.70  2852.5
4  14212.37  104364.2    1300  17499.87  105626.70  2867.5
5  14212.37  104364.2    1300  17499.87  105676.70  2867.5
6  14212.37  104364.2    1300  17524.87  105701.70  2867.5
7  14212.37  104364.2    1300  16762.74  107370.32  2882.5
8  14212.37  104364.2    1300  16753.10  107372.96  2897.5

我只想選擇其中一組坐標的x和y小於XC和YC列的數據幀同一行的12.5的行。

我努力了:

c = (x3,y3)

for i in c:
    df1 = (bf.loc[(bf['XC']-i <= abs(12.5))] & (bf['YC'] - i <= abs(12.5)))

print(df1)

但沒有得到理想的結果。

理想的結果將是:

print (df)
     XMORIG    YMORIG  ZMORIG        XC         YC      ZC
0  14212.37  104364.2    1300  16774.83  107357.67  2852.5
1  14212.37  104364.2    1300  16762.74  107370.32  2882.5
2  14212.37  104364.2    1300  16753.10  107372.96  2897.5

您可以壓縮僵屍列表和過濾器列表修真的列表DataFrame秒,然后concat在一起,也改變絕對值為系列的區別ij值,如果必要的:

x = (16764.83, 16752.74, 16743.1)
y = (107347.67, 107360.32, 107362.96)

dfs = [(bf[(bf['XC']-i) <= 12.5 & ((bf['YC'] - j) <= 12.5)]) for i, j in zip(x, y)]
#if necessary absolute values of difference Series
#dfs = [(bf[((bf['XC']-i).abs()<=12.5)&((bf['YC']-j).abs()<=12.5)]) for i, j in zip(x, y)]

df = pd.concat(dfs, ignore_index=True)
print (df)
     XMORIG    YMORIG  ZMORIG        XC         YC      ZC
0  14212.37  104364.2    1300  16774.83  107357.67  2852.5
1  14212.37  104364.2    1300  16762.74  107370.32  2882.5
2  14212.37  104364.2    1300  16753.10  107372.96  2897.5

暫無
暫無

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

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