How to convert pandas dataframe so that index is a unique set of values ​​and data is the amount of each value?

I have dataframefrom multiple choice questions and it is formatted like this:

      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

Data is a rating of 1 to 5 for three multiple choice questions. I want to reorder the data so that the index is in the range (1,6), where 1 = "bad", 2 = "bad", 3 = "ok", 4 = "good", 5 = "excellent", the columns are the same thing and data - this is a count of the number of occurrences of values ​​(excluding the "Sex" column). This is basically a histogram of fixed hopper sizes and an x ​​axis marked with lines. I like the output df.plot()much better than df.hist()that, but I can't figure out how to modify the table to give me a data histogram. Also, how do you change x-tags as strings?

+5
source share
1 answer

Series.value_counts gives you the histogram you are looking for:

In [9]: df['Qu1'].value_counts()
Out[9]: 
4    2
3    2
1    1

, :

In [13]: table = df[['Qu1', 'Qu2', 'Qu3']].apply(lambda x: x.value_counts())

In [14]: table
Out[14]: 
   Qu1  Qu2  Qu3
1    1    1    1
2  NaN    2    1
3    2    2  NaN
4    2  NaN    2
5  NaN  NaN    1

In [15]: table = table.fillna(0)

In [16]: table
Out[16]: 
   Qu1  Qu2  Qu3
1    1    1    1
2    0    2    1
3    2    2    0
4    2    0    2
5    0    0    1

table.reindex table.ix[some_array], .

, table.rename:

In [17]: table.rename(index=str)
Out[17]: 
   Qu1  Qu2  Qu3
1    1    1    1
2    0    2    1
3    2    2    0
4    2    0    2
5    0    0    1

In [18]: table.rename(index=str).index[0]
Out[18]: '1'
+17

All Articles