0

I am trying to convert particular values in given rows of a column into specific strings.

I want the value 1 to be converted to 'never', the value 2 to be converted to 'former' and the value 3 to be converted to 'current'.

However, when I run the following code, I get the error: 'list' object has no attribute 'loc'.

newdata = newdf.groupby('smoking-number').mean().reset_index()
sem = newdf.groupby('smoking-number').sem().reset_index()
newdata = newdata.loc[(newdata[['smoking_number']]==1) & (newdata[['smoking_number']]==2) & (newdata[['smoking_number']]==3)] = [['never','former','current']]

Would anybody be able to give me a helping hand?

1 Answer 1

1

A map should work:

newdata['smoking_number'] = newdata['smoking_number'].map({1: 'never', 2: 'former', 3: 'current'})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks!! However, when I add that line, it now gives me the error: 'str' object has no attribute 'view'. Why might be this be?
It should be related to something else. Do you have a function view called improperly somewhere in your code ?
It's pointing to this line: newdata.plot.line(x='smoking-number',y='distance', yerr= sem, ax=axes[0],legend=False)
It seems like you want smoking-number to be a category column, do you want to use a line plot ? Maybe a bar plot is more suitable ? I'm not sure if you can line plot against a column that contains strings
sure - I will try a barplot! :) Thanks so much for your help

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.