Home > Enterprise >  How to convert a dataframe to an array of counts (based on a column)
How to convert a dataframe to an array of counts (based on a column)

Time:04-20

I have the following dataframe

Line    emotion
0   d...    anger
1   a ...   shame
2   b...    sadness
3   c...    joy
4   d...    shame
... ... ...
117 f...    joy
118 g...    disgust
119 h...    disgust
120 i...    fear
121 j   anger

And I need to draw a Radar with the emotions like this:

import numpy as np
import matplotlib.pyplot as plt

categories = ['Joy', 'Fear', 'Anger', 'Sadness', 'Disgust', 'Shame','Guilt']
q1 = [4, 4, 5, 4, 3, 7, 10]
label_loc = np.linspace(start=0, stop=2*np.pi, num=len(q1) 1)[:-1]
plt.figure(figsize=(8, 8))
plt.subplot(polar=True)
plt.plot(label_loc, q1, label='q 1')
plt.title('Answer to Question 1 - Emotion Analysis', size=20)
lines, labels = plt.thetagrids(np.degrees(label_loc), labels=categories)
plt.legend()
plt.show()

Which result is:

enter code here

My question is, how can I easily conver the pandas dataframe to an array of COUNTS for each specific emotion:

 q1 = [4, 4, 5, 4, 3, 7, 10]

where each number represents these emotions:

categories = ['Joy', 'Fear', 'Anger', 'Sadness', 'Disgust', 'Shame','Guilt']

CodePudding user response:

Use Series.value_counts with convert values and index to lists:

s = df['emotion'].value_counts()
q1 = s.to_list()
categories = s.index.tolist()
print (q1)
[2, 2, 2, 2, 1, 1]

print (categories)
['joy', 'anger', 'shame', 'disgust', 'sadness', 'fear']

If ordering is important convert values to lowercase and add Series.reindex:

categories = ['Joy', 'Fear', 'Anger', 'Sadness', 'Disgust', 'Shame','Guilt']

cats = [x.lower() for x in categories]
q1 = df['emotion'].value_counts().reindex(cats, fill_value=0).tolist()
print (q1)
[2, 1, 2, 1, 2, 2, 0]
  • Related