How to apply lambda function on selected row only in Pandas Dataframe ?

I have a dataframe like this :

​

Namelast name
JohnDonald
Tom Petter

I want to select all the row with Name is NaN, then apply lambda function to get a string from last name to replace the NaN value.

I have try:

df.loc[df['Name'].isnull() ] = df.loc[df['Name'].isnull()].apply( lambda x  : (x['last name'].split(' ')[0]) ,axis=1)
                                                                                               

But it return error : Must have equal len keys and value when setting with an iterable

Posted May 11, 2020, 1:17 PM UTC

00

No comments yet

Nobody commented on this post, or the comments weren't captured.