Skip to content
Advertisement

pandas: Convert string column to ordered Category?

I’m working with pandas for the first time. I have a column with survey responses in, which can take ‘strongly agree’, ‘agree’, ‘disagree’, ‘strongly disagree’, and ‘neither’ values.

This is the output of describe() and value_counts() for the column:

JavaScript

I want to do a linear regression on this question versus overall score. However, I have a feeling that I should convert the column into a Category variable first, given that it’s inherently ordered. Is this correct? If so, how should I do this?

I’ve tried this:

JavaScript

This produces output that looks vaguely right, but it seems that the categories are in the wrong order. Is there a way that I can specify ordering? Do I even need to specify ordering?

This is the rest of my code right now:

JavaScript

Advertisement

Answer

Yes you should convert it to categorical data and this should do the trick

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement