I have a dataframe from a multiple choice questions and it is formatted like so:
Sex Qu1 Qu2 Qu3
Name
Bob M 1 2 1
John M 3 3 5
Alex M 4 1 2
Jen F 3 2 4
Mary F 4 3 4
The data is a rating from 1 to 5 for the 3 multiple choice questions. I want rearrange the data so that the index is range(1,6) where 1=’bad’, 2=’poor’, 3=’ok’, 4=’good’, 5=’excellent’, the columns are the same and the data is the count of the number occurrences of the values (excluding the Sex column). This is basically a histogram of fixed bin sizes and the x-axis labeled with strings. I like the output of df.plot() much better than df.hist() for this but I can’t figure out how to rearrange the table to give me a histogram of data. Also, how do you change x-labels to be strings?
Series.value_countsgives you the histogram you’re looking for:So, apply this function to each of those 3 columns:
Using
table.reindexortable.ix[some_array]you can rearrange the data.To transform to strings, use table.rename: