Title: Using List Comprehensions With Pandas Slug: pandas_list_comprehension Summary: Using List Comprehensions With Pandas Date: 2016-05-01 12:00 Category: Python Tags: Data Wrangling Authors: Chris Albon

Preliminaries


In [1]:
# Import modules
import pandas as pd

# Set ipython's max row display
pd.set_option('display.max_row', 1000)

# Set iPython's max column width to 50
pd.set_option('display.max_columns', 50)

Create an example dataframe


In [2]:
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'year': [2012, 2012, 2013, 2014, 2014], 
        'reports': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df


Out[2]:
name reports year
Cochice Jason 4 2012
Pima Molly 24 2012
Santa Cruz Tina 31 2013
Maricopa Jake 2 2014
Yuma Amy 3 2014

List Comprehensions

As a loop


In [3]:
# Create a variable
next_year = []

# For each row in df.years,
for row in df['year']:
    # Add 1 to the row and append it to next_year
    next_year.append(row + 1)

# Create df.next_year
df['next_year'] = next_year

# View the dataframe
df


Out[3]:
name reports year next_year
Cochice Jason 4 2012 2013
Pima Molly 24 2012 2013
Santa Cruz Tina 31 2013 2014
Maricopa Jake 2 2014 2015
Yuma Amy 3 2014 2015

As list comprehension


In [4]:
# Subtract 1 from row, for each row in df.year
df['previous_year'] = [row-1 for row in df['year']]

In [5]:
df


Out[5]:
name reports year next_year previous_year
Cochice Jason 4 2012 2013 2011
Pima Molly 24 2012 2013 2011
Santa Cruz Tina 31 2013 2014 2012
Maricopa Jake 2 2014 2015 2013
Yuma Amy 3 2014 2015 2013