Pandas dataframe apply lambda to selected rows only (based on a condition) within the dataframe

Question:

In the line of code below, I’m trying to apply a lambda formula only to selected rows based on a condition. I don’t want the formula to apply to every row in the dataset. The code seems to be working correctly but I’m getting a warning that says “SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame” (not sure why?) so I’m not sure if this is the right way to do it? Also if there’s a more efficient/easier way I’ll appreciate letting me know about it. Thanks.

I’m basically trying to say if the column GlobalName = ” THEN apply lambda (which has its own if statement)

df['GlobalName'][df['GlobalName']==''] = df['IsPerson'].apply(lambda x: x if x==True else '')
Asked By: Chadee Fouad

||

Answers:

According to the documentation, it seems like the correct notation is for the left of the = is:

df.GlobalName[df.loc[:, 'GlobalName'].eq('')]

Maybe try that and see if you get the warning

Answered By: oppressionslayer

Update: This solution works:

df['GlobalName'] = np.where(df['GlobalName']=='', df['IsPerson'].apply(lambda x: x if x==True else ''), df['GlobalName'])
Answered By: Chadee Fouad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.