1

suppose I have a dataframe df. I want to create a new column, HighRenew and define its value as : " if the country's % Renewable value is at or above the median for all countries in the top 15 the value must be 1, else it must be a 0" this is the code i wrote :

df = answer_one()['% Renewable'].to_frame()
med = np.median(df)
df['HighRenew'] = 1 if df['% Renewable'] >= med else 0

when i run this, it raises the following error :

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

  • i have the read this post as well but have not find any solutions.
  • i know i can just use .apply() and get the same result. but i was wondering if there's any equivalent in an idiomatic pandas way

i haven't find anything specific to this problem on SO or official documents as well

1 Answers1

1

Another way to return chosen elements from a condition is using np.where().

df['HighRenew'] = np.where(df['% Renewable'] >= med, 1, 0)

Or the same could be achieved with a list comprehension, but comprehensions can be slower. df['HighRenew'] = [1 if i >= med else 0 for df['% Renewable']]

Beth_light
  • 26
  • 3