Ptar
Ptar

Reputation: 374

Why am I not able to apply pandas.cut() on these lines of code?

points = [1,2,3,4,5,6,7,8,9,10,11,12]
bins = [0,30,35,40,45,50,55,60,65,70,75,80,100]
df1['HIS'] = pd.cut(df1.HIS,bins,labels=points)
df1['GEO'] = pd.cut(df1.GEO,bins,labels=points)
df1['CRE'] = pd.cut(df1.CRE,bins,labels=points)
df1['AGR'] = pd.cut(df1.AGR,bins,labels=points)

Only the above lines are 'cut-able'/executable as when the line below is run

df['PTS'] = df1[['BIO', 'PHY']].max(axis=1) + df1[['HIS','GEO','CRE','AGR','H/SC','BST']].apply(lambda row: row.sort_values(ascending=False).head(2).sum() ,axis=1) + df1['ENG'] + df1['KIS']+ df1['MAT']+ df1['CHE']

I observed and saw that only a certain portion from this second code is successful. Trying to .cut()

df1['PHY'] = pd.cut(df1.PHY,bins,labels=points)

or

df1['ENG'] = pd.cut(df1.ENG,bins,labels=points)

returns the following error...

TypeError: Object with dtype category cannot perform the numpy op add

I'm not able to find where to correct. df.dtypes =

ENG        int64
KIS        int64
MAT        int64
BIO        int64
PHY        int64
CHE        int64
HIS     category
GEO     category
CRE     category
H/SC       int64
AGR     category
BST     category
dtype: object

Upvotes: 1

Views: 497

Answers (1)

NYC Coder
NYC Coder

Reputation: 7594

You need to change the dtype of the category columns to string or int64 or object. Hard to say without looking at the data.

Upvotes: 1

Related Questions