Pandas Slice Rows Based On Joint Condition
consider the below dataframe -df one two three four five six seven eight 0 0.1 1.1 2.2 3.3 3.6 4.1 0.0 0.0 1 0.1 2.1 2.3 3.2 3.7 4.3 0.0
Solution 1:
You can do the following in short:
df.eq(3.2).any(axis=1) & ~df.isin([0.1, 1.2]).any(axis=1)
Or here more explicitly:
contains = df.eq(3.2).any(axis=1)
not_contains = ~df.isin([0.1,1.2]).any(axis=1)
print(df[contains & not_contains])
one two three four five six seven eight
52.13.20.00.00.00.00.00.062.12.33.24.30.00.00.00.0
Solution 2:
For performance, specially since you mentioned large dataset and if you are looking to exclude just two numbers, here's one approach with array data -
a = df.values
df_out = df.iloc[(a == 3.2).any(1) & (((a!=0.1) & (a!=1.2)).all(1))]
Sample run -
In [43]: a = df.values
In [44]: df.iloc[(a ==3.2).any(1) & (((a!=0.1) & (a!=1.2)).all(1))]
Out[44]:
one two three four five six seven eight
52.13.20.00.0000062.12.33.24.30000
Solution 3:
You could just combine the conditions.
>>>df[(df == 3.2).any(1) & ~df.isin([0.1, 1.2]).any(1)]
one two three four five six seven eight
5 2.1 3.2 0.0 0.0 0.0 0.0 0.0 0.0
6 2.1 2.3 3.2 4.3 0.0 0.0 0.0 0.0
Post a Comment for "Pandas Slice Rows Based On Joint Condition"