2
$\begingroup$

I'm a data science student at Mills and working on CoCalc. I am working with the 'wine quality' set that can be found on the UCI website for my final project. I'm just working with the white wine portion. The target is the quality of the wine, rated from 1-10. I would like to change the values in the column 'quality' such that if the value is from 1-4, it is replaced with 'low', if the value is from 5-6, the value is replaced with 'medium', and if the value is 8-10, it is replaced with 'high'. My code is as such:

#Read in data

white = pd.read_csv('Wine_data.csv')

#Rename columns to exlude the u

columns = ['fixed acidity', 'volatile acidity', 'citric acid','residual sugar', 'chlorides', 'free sulfur dioxide','total sulfur dioxide', 'density', 'pH', 'sulphates', 'alcohol', 'quality']
white.columns = columns

quality = np.array(white['quality'])

for i in range(len(quality)):
    if quality[i] in [1,2,3,4]:
        quality[i] = 'low'
    elif quality[i] in [5,6,7]:
        quality[i] = 'med'
    else:
        quality[i] = 'high'

However, I am getting this error:

Error in lines 13-19
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 5, in <module>
ValueError: invalid literal for long() with base 10: 'med'

Here is a link to my code.

Help this nOOb please.

$\endgroup$

2 Answers 2

3
$\begingroup$

Assuming that the data is small enough that efficiency is not an issue, I would use the Pandas data frame and do something like:

white['quality_code'] = 'high'
white.loc[quality < 5, 'quality_code'] = 'low'
white.loc[(quality > 4) & (quality < 8) , 'quality_code'] = 'med'
$\endgroup$
2
$\begingroup$

Looks like smc_sagews requires numeric values.

You should replace strings low/med/high with numbers 0/1/2 or -1/0/1.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.