0

I am very new to python and trying to implement if else logic and assign it to a new column.

My sample data looks like this

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.

unicorn
  • 496
  • 6
  • 20
  • 1
    Possble duplicate of [*Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()*](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Alexandre B. Aug 03 '19 at 14:17
  • i saw this.. i am using & and not and. – unicorn Aug 04 '19 at 02:29

0 Answers0