Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my dataframe.

I've tried different methods from other questions but still can't seem to find the right answer for my problem. The critical piece of this is that if the person is counted as Hispanic they can't be counted as anything else. Even if they have a "1" in another ethnicity column they still are counted as Hispanic not two or more races. Similarly, if the sum of all the ERI columns is greater than 1 they are counted as two or more races and can't be counted as a unique ethnicity(except for Hispanic). Hopefully, this makes sense. Any help will be greatly appreciated.

Its almost like doing a for loop through each row and if each record meets a criterion they are added to one list and eliminated from the original.

1 Answer

0 votes
by (106k points)

To create new column based on values from other columns in pandas you need two steps to this - first is to write a function that does the translation you want - I've put an example together based on your pseudo-code:

def label_race (row): 

if row['eri_hispanic'] == 1 : 

return 'Hispanic' 

if row['eri_afr_amer'] + row['eri_asian'] + row['eri_hawaiian'] + row['eri_nat_amer'] + row['eri_white'] > 1 : 

return 'Two Or More' 

if row['eri_nat_amer'] == 1 : 

return 'A/I AK Native' 

if row['eri_asian'] == 1: 

return 'Asian' 

if row['eri_afr_amer'] == 1:

return 'Black/AA' 

if row['eri_hawaiian'] == 1:

return 'Haw/Pac Isl.' 

if row['eri_white'] == 1:

return 'White' 

return 'Other'

Browse Categories

...