1# Creates a new column 'blue_yn' based on the existing 'color' column
2# If the 'color' column value is 'blue' then the new column value is 'YES'
3df['blue_yn'] = np.where(df['color'] == 'blue', 'YES', 'NO')
4# Can also do this using .apply and a lambda function
5df['blue_yn']= df['color'].apply(lambda x: 'YES' if (x == 'blue') else 'NO')
1def label_race (row):
2 if row['eri_hispanic'] == 1 :
3 return 'Hispanic'
4 if row['eri_afr_amer'] + row['eri_asian'] + row['eri_hawaiian'] + row['eri_nat_amer'] + row['eri_white'] > 1 :
5 return 'Two Or More'
6 if row['eri_nat_amer'] == 1 :
7 return 'A/I AK Native'
8 if row['eri_asian'] == 1:
9 return 'Asian'
10 if row['eri_afr_amer'] == 1:
11 return 'Black/AA'
12 if row['eri_hawaiian'] == 1:
13 return 'Haw/Pac Isl.'
14 if row['eri_white'] == 1:
15 return 'White'
16 return 'Other'
17
18df.apply(lambda row: label_race(row), axis=1)