Unable to form categorical groups

python
technique

#1

I am unable to convert continuous data into categorical data.
Below is the snippet -

train.loc[train[‘income_spend’]<=0.5,‘income_spend’]=0
train.loc[(train[‘income_spend’]>0.5) & (train[‘income_spend’]<=1.0),‘income_spend’]=1
train.loc[(train[‘income_spend’]>1.0) & (train[‘income_spend’]<=1.5),‘income_spend’]=2
train.loc[(train[‘income_spend’]>1.5) & (train[‘income_spend’]<=2.5),‘income_spend’]=3
train.loc[train[‘income_spend’]>2.5,‘income_spend’]=4

On checking the value counts, group 0 and 1 are properly formed but rest all entries merged into group 4.

Same is the problem with 2 other features as well.


#2

Hi @ASHISH_17

You can try doing the same using the pandas cut function like shown below.

train['income_spend'] = pd.cut(train['income_spend'], bins = [0,0.5,1,1.5,2.5,np.max(train['income_spend'])], labels = ['0','1','2','3','4'] )

Hope this helps.
Shubham


#3

Hello,

I have tried using your method on a different column having discrete values.
This is the error showing -
TypeError: ufunc ‘subtract’ did not contain a loop with signature matching types dtype(‘S11’) dtype(‘S11’) dtype(‘S11’)


#4

Even now a new problem arises that bins must increase monotonically.

How to get rid of it?