Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading