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.