简体   繁体   中英

Multiple conditions - Selecting rows in pandas dataframe

i have x and y coordinates as:

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

its basically like three points (x1, y1), (x2, y2) and (x3, y3)

in the dataframe:

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

i want to choose only those rows in which the x and y of one set of coordinates are less than 12.5 of the same row of the dataframe from column XC and YC.

i have tried:

c = (x3,y3)

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

print(df1)

but not getting the desired result.

The desired outcome would be :

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

You can zip bot list and filter in list comprehension for list of DataFrame s and then concat together, also change absolute values for Series with difference of i and j values if necessary:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM