Title: Create a Column Based on a Conditional in pandas
Slug: pandas_create_column_using_conditional
Summary: Create a Column Based on a Conditional in pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon

Preliminaries


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

Make a dataframe


In [2]:
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'age': [42, 52, 36, 24, 73], 
        'preTestScore': [4, 24, 31, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(data, columns = ['name', 'age', 'preTestScore', 'postTestScore'])
df


Out[2]:
name age preTestScore postTestScore
0 Jason 42 4 25
1 Molly 52 24 94
2 Tina 36 31 57
3 Jake 24 2 62
4 Amy 73 3 70

Add a new column for elderly


In [3]:
# Create a new column called df.elderly where the value is yes
# if df.age is greater than 50 and no if not
df['elderly'] = np.where(df['age']>=50, 'yes', 'no')

In [4]:
# View the dataframe
df


Out[4]:
name age preTestScore postTestScore elderly
0 Jason 42 4 25 no
1 Molly 52 24 94 yes
2 Tina 36 31 57 no
3 Jake 24 2 62 no
4 Amy 73 3 70 yes