Title: Filter pandas Dataframes
Slug: filter_dataframes
Summary: Filter pandas Dataframes
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [8]:
import pandas as pd
In [9]:
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'year': [2012, 2012, 2013, 2014, 2014],
'reports': [4, 24, 31, 2, 3],
'coverage': [25, 94, 57, 62, 70]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df
Out[9]:
In [10]:
df['name']
Out[10]:
In [11]:
df[['name', 'reports']]
Out[11]:
In [12]:
df[:2]
Out[12]:
In [13]:
df[df['coverage'] > 50]
Out[13]:
In [14]:
df[(df['coverage'] > 50) & (df['reports'] < 4)]
Out[14]: