Answers for "pandas notin"

5

pandas not in list

>>> df
  countries
0        US
1        UK
2   Germany
3     China
>>> countries
['UK', 'China']
>>> df.countries.isin(countries)
0    False
1     True
2    False
3     True
Name: countries, dtype: bool
>>> df[df.countries.isin(countries)]
  countries
1        UK
3     China
>>> df[~df.countries.isin(countries)]
  countries
0        US
2   Germany
Posted by: Guest on March-27-2020
0

pandas isin

# Filter dataframe by multiple values in a column or multiple columns
# SQL equivalent: WHERE columnA IN ('val1', 'val2') AND columnB > 70

df[ (df.columnA.isin(['val1', 'val2'])) & (df['columnB'] > 70) ]
Posted by: Guest on August-10-2021
0

isnotin python

import pandas as pd

>>> df
  country
0        US
1        UK
2   Germany
3     China
>>> countries_to_keep
['UK', 'China']
>>> df.country.isin(countries_to_keep)
0    False
1     True
2    False
3     True
Name: country, dtype: bool
>>> df[df.country.isin(countries_to_keep)]
  country
1        UK
3     China
>>> df[~df.country.isin(countries_to_keep)]
  country
0        US
2   Germany
Posted by: Guest on July-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language