Pandas dataframe list comprehension multiple columns

Learning machine learning with machine learning flashcards, Python ML book, or study with me videos.

Using List Comprehensions With pandas

20 Dec 2017

Preliminaries

# 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

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
namereportsyear
CochiceJason42012
PimaMolly242012
Santa CruzTina312013
MaricopaJake22014
YumaAmy32014

List Comprehensions

As a loop

# 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
namereportsyearnext_year
CochiceJason420122013
PimaMolly2420122013
Santa CruzTina3120132014
MaricopaJake220142015
YumaAmy320142015

As list comprehension

# Subtract 1 from row, for each row in df.year df['previous_year'] = [row-1 for row in df['year']]
df
namereportsyearnext_yearprevious_year
CochiceJason4201220132011
PimaMolly24201220132011
Santa CruzTina31201320142012
MaricopaJake2201420152013
YumaAmy3201420152013

Find an error or bug?

Everything on this site is available on GitHub. Head to and submit a change.