Drop rows if x,y and z coordinates are inside a geometric box (pandas)
Question:
I have a pandas dataframe that has columns of ‘x’, ‘y’ and ‘z’ coordinates. I would like to find a way to create a new dataframe out of this one that would not include rows with coordinates if they match this pattern (all of the 3 must be false):
-1<x<1, -1<y<1.5, -2.5<z<2.5
Here is a quick example with the dataframe at the begining:
y x z
0 0.8 0.5 2
1 1 -0.3 0
2 1 -0.3 0
3 -0.4 1.5 0
4 0 0.2 1
And the expected output:
y x z
1 1 -0.3 0
2 1 -0.3 0
3 -0.4 1.5 0
Does anyone has a quick way to do it?
Thanks a lot!
Cheers
Answers:
It seems to me that your question doesn’t match your example? You mixed up X Y Z in your constraints and your example… Anyway, this is what you are looking for. "&" is "and" while "|" would be "or" and "~" is a negation. You will need brackets for any such statements… something I had to learn back then in hours of debugging.
df = pd.DataFrame([
[0.8, 0.5, 2],
[1, -0.3, 0],
[1, -0.3, 0],
[-0.4, 1.5, 0],
[0, 0.2, 1]
], columns=["X", "Y", "Z"])
df[
~(
(df.X > -1) &
(df.X < 1) &
(df.Y > -1) &
(df.Y < 1.5) &
(df.Z >-2.5) &
(df.Z < 2.5)
)
]
print(df)
Output matches your excepted output when column names are changed from Y X Z
is changed to X Y Z
.
X Y Z
1 1.0 -0.3 0
2 1.0 -0.3 0
3 -0.4 1.5 0
(I guess you made a mistake in your question?)
I have a pandas dataframe that has columns of ‘x’, ‘y’ and ‘z’ coordinates. I would like to find a way to create a new dataframe out of this one that would not include rows with coordinates if they match this pattern (all of the 3 must be false):
-1<x<1, -1<y<1.5, -2.5<z<2.5
Here is a quick example with the dataframe at the begining:
y x z
0 0.8 0.5 2
1 1 -0.3 0
2 1 -0.3 0
3 -0.4 1.5 0
4 0 0.2 1
And the expected output:
y x z
1 1 -0.3 0
2 1 -0.3 0
3 -0.4 1.5 0
Does anyone has a quick way to do it?
Thanks a lot!
Cheers
It seems to me that your question doesn’t match your example? You mixed up X Y Z in your constraints and your example… Anyway, this is what you are looking for. "&" is "and" while "|" would be "or" and "~" is a negation. You will need brackets for any such statements… something I had to learn back then in hours of debugging.
df = pd.DataFrame([
[0.8, 0.5, 2],
[1, -0.3, 0],
[1, -0.3, 0],
[-0.4, 1.5, 0],
[0, 0.2, 1]
], columns=["X", "Y", "Z"])
df[
~(
(df.X > -1) &
(df.X < 1) &
(df.Y > -1) &
(df.Y < 1.5) &
(df.Z >-2.5) &
(df.Z < 2.5)
)
]
print(df)
Output matches your excepted output when column names are changed from Y X Z
is changed to X Y Z
.
X Y Z
1 1.0 -0.3 0
2 1.0 -0.3 0
3 -0.4 1.5 0
(I guess you made a mistake in your question?)