Reputation: 715
I have the following scenario where I need to fill my empty column value with another column value.
my.csv
country newCountry
France Argentina
Uruguay
Germany Ireland
desired output:
country newCountry
France Argentina
Uruguay Uruguay
Germany Ireland
my code:
df.loc[df['newCountry'] == '', 'newCountry'] = df['country']
it doesn't throw any error, but after running this the row value remains empty instead of show Uruguay in the newCountry column.
Could someone help with this please?
Upvotes: 2
Views: 6396
Reputation: 863341
If possible mutiple spaces use Series.str.strip
:
df.loc[df['newCountry'].str.strip() == '', 'newCountry'] = df['country']
Or if there are missing values instead empty space use Series.fillna
:
df['newCountry'] = df['newCountry'].fillna(df['country'])
Upvotes: 2