I have the following dataframe:
cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','No Brand'],
'Country': ['Japan','No Country','United States','Germany']
}
df = pd.DataFrame(cars, columns = ['Brand', 'Country'])
df.head(4)
Brand Country
0 Honda Civic Japan
1 Toyota Corolla No Country
2 Ford Focus United States
3 No Brand Germany
Would like to create a new column in the dataframe which will combine based on the values of column 'Brand' and 'Country'. If there is 'No Brand' value in column Brand then column Desc only takes the value in column Country. If there is 'No Country' value in column Country then column Desc only takes the value in column Brand. Desired output:
Brand Country Desc
0 Honda Civic Japan Honda Civic Japan
1 Toyota Corolla No Country Toyota Corolla
2 Ford Focus United States Ford Focus United States
3 No Brand Germany Germany
If it is checking the string in one column, I am able to do so but not sure how to proceed for two columns. Right now I can only check the boolean on the condition I want.
df['Desc'] = df['Brand'].str.contains("No Brand") | df['Country'].str.contains("No Country")
Brand Country Desc
0 Honda Civic Japan False
1 Toyota Corolla No Country True
2 Ford Focus United States False
3 No Brand Germany True
I read that it is not recommended to iterate dataframe and avoid doing so.