I am very new to python and trying to implement if else logic and assign it to a new column.
I am trying to create a new column Quantile and check which group the "2014-15" data falls .
I tried with Funtion.
def func(row):
if ((df1['2014-15']>df1.quantile([0.85]))& (df1['2014-15'] <=df1.quantile([1]))):
return 'C1'
elif ((df1['2014-15']>df1.quantile([0.5]))& (df1['2014-15']<= df1.quantile([0.85]))):
return 'C2'
elif ((df1['2014-15']> df1.quantile([0.20])) & (df1['2014-15'] <= df1.quantile([0.50]))):
return 'C3'
elif df1['2014-15']<=df1.quantile([0.20]):
return 'C4'
df1['Quantile'] = df1.apply(func,axis=1)
But this throws an error for me.
ValueError: ('The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 32')
Then i tried with np.where :
np.where(((df1['2014-15']>df1.quantile([0.85]))& (df1['2014-15'] <=df1.quantile([1]))), 'C1')
I get below error: either both or neither of x and y should be given
Can anyone please help me how do i get a new column and assign the quantile group?
any value between 0.85 and 1 should be C1, anything between 0.85 and 0.5 should be C2, anything between 0.5 and 0.85 should be C3 and anything lesser than 0.2 should be C4. These are the quantile ranges.