Pandas Column Filter Code Example

Snippet 1

  # filter rows in a dataframe by a condition on a column

df_filtered = df.loc[df['column'] == value] 

Snippet 2

  >continents = ['Asia','Africa', 'Americas', 'Europe']
>gapminder_Ocean = gapminder[~gapminder.continent.isin(continents)]
>gapminder_Ocean.shape 
(24,6)
 

Snippet 3

  filter = df['column']!= 0
df = df[filter] 

Snippet 4

  # filter rows for year 2002 using  the boolean expression
>gapminder_2002 = gapminder[gapminder.year.eq(2002)]
>print(gapminder_2002.shape)
(142, 6) 

Copyright © Code Fetcher 2020

 

 

Leave a comment

Your email address will not be published. Required fields are marked *