I have a DataFrame that looks like this...
Variable 0 Religion - Buddhism 1 Source: Clickerz 2 Religion - Islam 3 Source: SRZ FREE 4 Ethnicity - Mixed - White & Black African I want to manipulate the variablecolumn to create a new column which looks like this...
Variable New Column 0 Religion - Buddhism Buddhism 1 Source: Clickerz Clickerz 2 Religion - Islam Islam 3 Source: SRZ FREE SRZ FREE 4 Ethnicity - Mixed - White & Black African Mixed - White and Black African So that I can eventually have a DataFrame that looks like this...
Variable New Column 0 Religion Buddhism 1 Source Clickerz 2 Religion Islam 3 Source SRZ FREE 4 Ethnicity Mixed - White and Black African I want to iterate through the Variable column and manipulate the data to create New Column. I was planning on using multiple if statements to find a specific word for example 'Ethnicity' or 'Religion' and then apply a manipulation.
For example...
For row in df['Variable']: if 'Religion' in row: df['New Column'] = ... elif 'Ethnicity' in row: df['New Column'] = ... elif: 'Source' in row: df['New Column'] = ... else: df['New Column'] = 'Not Applicable' Even though type(row) returns 'str' meaning it is of the class string, this code keeps returning the new column as all 'Not Applicable' meaning it is not detecting any of the strings in any of the rows in the data frame even when I can see they are there.
I am sure there is an easy way to do this...PLEASE HELP!
I have tried the following aswell...
For row in df['Variable']: if row.find('Religion') != -1: df['New Column'] = ... elif row.find('Ethnicity') != -1: df['New Column'] = ... elif: row.find('Source') != -1: df['New Column'] = ... else: df['New Column'] = 'Not Applicable' And I continue to get all entries of the new column being 'Not Applicable'. Once again it is not finding the string in the existing column.
Is it an issue with the data type or something?