0

my question is simply how would I change the X-axisOUTCOME OF CODE FOR BAR CHART

from the numbers (Ex. 17, 1, 2, 4) to the PET names (Ex. Horse, Dog, etc). Code Below

I'm new to stack overflow but here is an image of the code. I'll eventually figure out how to use it.

Here is my code.

CSV DOWN BELOW

Height,Ice Cream,Pet,#of letters,Favorite TA,Minutes to Hometown
67,Chocolate,Dog,16,Ryan,55
63,Cookie Dough,None,15,Mimi,180
75,Vanilla,Dog,20,Peter,150
66,Moose Tracks,Dog,16,Erik,300
72,Vanilla,Dog,17,Ryan,60
66,Moose Tracks,Dog,14,Emily,85
72,Chocolate,None,21,Josh,70
72,Mint Chip,Cat,9,Abe,70
71,Vanilla,Dog,20,Brian,90
75,Vanilla,Dog,5,None,105
68,Strawberry,Horse,19,Emily,120
59,Chocolate,Dog,13,Cezanne,45
73,Vanilla,Dog,14,Monica,155
64,Chocolate,None,16,Sarah,120
67,Mint Chip,Dog,21,Kelly,180
67,Mint Chip,Dog,20,Ryan,90
68,Cookies & Cream,Dog,13,Toner,180
65,Chocolate,Dog,20,None,60
75,Chocolate,Dog,9,Chris,120
67,Cookie Dough,None,10,Katie,75
67,Vanilla,Dog,24,Carlos,120
75,Mint Chip,Dog,17,Tim,180
64,Vanilla,Dog,15,Xavier,135
74,Cookies & Cream,Horse,18,Jeff,180

Code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

h2 = pd.read_csv("hw_2_data.csv")

## create grouped dataframe
h2Grouped = h2.groupby('Pet').count()
##h2Grouped

##recreate serverity variable
h2Grouped['Pet'] = h2Grouped.index
## rename the variables
h2Grouped.columns = ['Count', 'Pet', 'Ice Cream', '#of letters', 'Favorite TA', 'Minutes to Hometown']

#matplot
%matplotlib inline

##bar
bar1 = h2Grouped.plot.bar(x = 'Pet', y = 'Count', title = 'Pet Bar Chart')
bar1.set_xlabel('Pet')

##get rid of text output
t = bar1.set_ylabel('Count')
3
  • df.groupby('Pet').count()['Height'].plot(kind='bar', rot=0) is all you need. The x-axis names are based on the index, not the column names. Commented Sep 3, 2021 at 4:57
  • df.Pet.value_counts().plot(kind='bar', rot=0) is even better. Commented Sep 3, 2021 at 5:00
  • Thank you guys very much it works perfectly now. Commented Sep 3, 2021 at 15:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.