First time here? Checkout the FAQ!
x
+2 votes
541 views
asked in Python by (660 points)  

Hello,

I am trying to gain insight of the data but having hard time plotting it.

Suppose I have a data frame like below

ID Feature 1 Class
1 a good
2 a good
3 b good
4 b bad
5 a bad
6 a bad
7 b bad

In the above dataframe I have one feature "a" and "b" each has a good and bad class value.

for e.g

feature "a" has 2 good and 2 bad classes 

feature "b" has 1 good and 2 bad classes

  • How do i visualize this and plot it in python ?
  • How would I do it for more than one features in different subplots?

Sorry if this question seems like a very basic question to ask 

  

2 Answers

+1 vote
answered by (660 points)  

I figured it out by using the matplot lib. What we do is following
Divide the dataframe in to two

df_good = df[df['Class'] == 'good'] 
df_bad = df[df['Class'] == 'bad']

Then group the dataframes with the desired features

df_good = df_good.groupby('Feature 1')
df_bad = df_bad.groupby('Feature 1')

Then get the size which will give us the series and then plot the bar graph

df_good.size().plot(kind='bar', color='blue', legend=True, label='class = good')
df_bad.size().plot(kind='bar', color='blue', legend=True, label='class = bad')

This worked for me and plotted a nice bar graph.

commented by (116k points)  
Thanks for sharing. Two notes:
1) Please use code section on the toolbar when sharing your codes.
2) That would be great to provide executable version on https://repl.it
0 votes
answered by (116k points)  

I think this url that shows how to aggregate and plot is what you are looking for. You have several options such as showing them in separate bar charts or stacking them up after aggregation. Another option is using multi-level pie chart or sunburst as you can see the example.

Related questions

+1 vote
1 answer 327 views
asked Oct 19, 2018 in Python by Neo (660 points)  
0 votes
1 answer 267 views
...