1
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
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
Ptar
  • 374
  • 4
  • 16
  • Your data maybe strings, what do you get when you do `df.dtypes`? You should try converting those columns into numerical values. – Quang Hoang May 15 '20 at 18:18
  • 1
    it's going to be really difficult to help you troubleshoot this without seeing the dataframe you're working with. For more info on how to ask a good pandas question, see: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Paul H May 15 '20 at 18:19
  • df.dtypes = object – Ptar May 15 '20 at 18:27

1 Answers1

1

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

NYC Coder
  • 7,424
  • 2
  • 11
  • 24