Using condition-based logic in group wise transformations
I have a dataframe of data that has a year column ('Year' and a dollar
value column. I want to group by the year, then for each row, determine if
the row is above the group's median by 20% or below the group's median by
20%.
I tried the following:
def f(x):
if x >= 1.2* np.median(x):
return 'H'
elif x<= .8* np.median(x):
return 'L'
transformed = df.groupby('Year').transform(f)
But I get an error saying the truth value of an array is ambiguous. This
makes me think python is treating x in both the left and right hand side
of the equation as the array of values, when in other transformation
functions it knows on the left hand side the x is the row element and on
the right hand side, where x is wrapped in an aggregation, x is the array.
Any idea on how to do this?
No comments:
Post a Comment