0

I have a dataframe which is survey answer, so the customer can give a score from 0 to 10 and a lot of them leaves the survey empty, but when I do df.fillna(''), I can not make math calculations and taking this error '<' not supported between instances of 'str' and 'int', so Im trying to convert this to float with df['Survey 1'].astype(float)but again could not convert string to float: '' or invalid literal for int() with base 10: '' so somehow after converting NaN 's to empty I have to change the type to float or int in order to make a calculation like this

survey_answers = [
(df['Survey 1']<7) & (df['Survey 1']!=''), 
(df['Survey 1']>8) & (df['Survey 1']<11),
(df['Survey 1']>6) & (df['Survey 1']<9)
    ]
results = ['Detractor','Promoter','Passive']
df['NPS Score'] = np.select(survey_answers, results)
Eren Han
  • 321
  • 3
  • 10

1 Answers1

0

Use .astype(int):

survey_answers = [
(df['Survey 1'].astype(int)<7) & (df['Survey 1']!=''), 
(df['Survey 1'].astype(int)>8) & (df['Survey 1'].astype(int)<11),
(df['Survey 1'].astype(int)>6) & (df['Survey 1'].astype(int)<9)
    ]
Wasif
  • 14,755
  • 3
  • 14
  • 34