ktl014
8/9/2017 - 8:38 PM

Create a Column Based on a Conditional in pandas from https://chrisalbon.com/python/pandas_create_column_using_conditional.html

Create a Column Based on a Conditional in pandas from https://chrisalbon.com/python/pandas_create_column_using_conditional.html

#Preliminaries

# Import required modules
import pandas as pd
import numpy as np

#Make a dataframe

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

#Add a new column for elderly

# 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')
# View the dataframe
df

#First create a python's list_of_e that has relevant data.
#Use this: 
df['e'] = list_of_e