Title: Applying Operations Over pandas Dataframes
Slug: pandas_apply_operations_to_dataframes
Summary: Applying Operations Over pandas Dataframes
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [1]:
import pandas as pd
import numpy as np
In [2]:
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[2]:
In [3]:
capitalizer = lambda x: x.upper()
In [4]:
df['name'].apply(capitalizer)
Out[4]:
In [5]:
df['name'].map(capitalizer)
Out[5]:
In [6]:
# Drop the string variable so that applymap() can run
df = df.drop('name', axis=1)
# Return the square root of every cell in the dataframe
df.applymap(np.sqrt)
Out[6]:
In [7]:
# create a function called times100
def times100(x):
# that, if x is a string,
if type(x) is str:
# just returns it untouched
return x
# but, if not, return it multiplied by 100
elif x:
return 100 * x
# and leave everything else
else:
return
In [8]:
df.applymap(times100)
Out[8]: