Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23305

Pandas "ValueError: The truth value of a Series is ambiguous." with Lambda

$
0
0

I have a df for which I want to patch the negative values (replace them with mean of the group).

I'm confused as to why the following throws an error:

df['patched_vals']=df.groupby(['grps'])['vals'].transform(lambda x: x if (x >0) else x.mean())

While this works fine:

df['patched_vals']=df.groupby('grps').vals.transform(lambda x: x +(x.mean()-x)*(x<0))

Viewing all articles
Browse latest Browse all 23305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>