Home > Enterprise >  Pandas dataframe ValueError: The truth value of a Series is ambiguous
Pandas dataframe ValueError: The truth value of a Series is ambiguous

Time:04-19

I have a dataframe which has 3884 rows × 4458 columns and filled with numbers. I'm trying to equate numbers greater than 1 to 1. I have tried

df.apply(lambda x: 1 if x >= 1 else 0)

Or I tried to make function but I'm getting this error.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I checked previous topics and see many questions about this but I really don't understand.

CodePudding user response:

It is because x >= 1 returns a series of True/False values based on the original numeric value, because x is a series representing a row inside your lambda.

You could use (x >= 1).all() or any() or such but that won't suit your needs.

Instead you may use the below to transform each value in the df:

df.apply(lambda x : [1 if e >= 1 else 0 for e in x])

CodePudding user response:

pandas.DataFrame.apply will apply a function along an axis of the DataFrame. Thus x in your lambda is a Series which couldn't be feed into if after comparing.

I'm trying to equate numbers greater than 1 to 1.

You can use pandas.DataFrame.applymap which apply a function to a Dataframe elementwise.

df.applymap(lambda x: 1 if x >= 1 else 0)
  • Related