Skip to content
Advertisement

Pandas: How to fill nan value for column with part of value in other columns

I Want the value in city column to be filled with first word of venue column

enter image description here

I tried using df.city.fillna(value=df.venue.str.split()[0]) but it taking first row values to fill Thank you in advance

Advertisement

Answer

From your DataFrame :

JavaScript

After the split() you used, we can use map to assign the first list element to the NaN values in the City column as expected :

JavaScript

EDIT:

Even shorter, thanks to @HenryEcker :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement