-1

I am supposed to create a new pandas columns by comparing the values of this column ('% Renewable') to the median of the same column. And the result should make up a new column.

Of course I could use a for loop to do this. Though I am only at the beginning of my learning I want to make more use of the map, lambda etc methods.

Therefore I tried this:

def above(x,y):
if x>=y:
    return 1
else:
    return 0

def answer_ten():
  Top15 = answer_one() #loads the dataframe and formats it

  Median=Top15['% Renewable'].median()

  Top15['HighRenew']=map(above, Top15['% Renewable'], Top15['% Renewable'].median()

# one try: list(map(above, (Top15['% Renewable'], Top15['% Renewable'].median())))  
# one more try: [*map(above, (Top15['% Renewable'], Top15['% Renewable'].median()))]  

return  Top15['HighRenew']

But instead of the value I get an error: 'float' object is not iterable

I tried to alternatives that are liste in the comment line which I got from one other post here: Getting a map() to return a list in Python 3.x

By now I figured out a different one-line solution like this:

Top15['HighRenew']=(Top15['% Renewable']>=Top15['% Renewable'].median()).astype('int')

But I would like to know how I could have this differently (of course more lenghty) with Lambda or map() or filter(?).

Could anyone point me towards an alternative solution?

Thanks.

Andreas K.
  • 389
  • 4
  • 17

2 Answers2

1

You probably just want above(Top15['% Renewable'], Top15['% Renewable'].median()). map takes a sequence of objects and applies the function to each one, but you only want to apply it once. The error you get is because the two values you pass in cannot be looped over.

internet_user
  • 3,149
  • 1
  • 20
  • 29
0

So you basically want something like this:

Top15['HighRenew'] = Top15.apply(lambda df: int(df['% Renewable'] >= Top15['% Renewable'].median()))
zipa
  • 27,316
  • 6
  • 40
  • 58