So I have a generated a Disease_Data dataframe that has 2 columns, Location and Data (see below).
JavaScript
x
8
1
Location Data
2
Alabama 30.675
3
Alaska 27.3917
4
Arizona 25.825
5
Arkansas 29.2
6
California 29.117
7
Colorado 35.65
8
I wanted to generate a bar graph like below:
However, when I tried the code below, things did not work and gave an error: KeyError: ‘Location’
JavaScript
1
7
1
fig = plt.figure()
2
ax = fig.add_axes([0,0,1,1])
3
Disease_Data_Loc = Disease_Data['Location']
4
Disease_Data_Value = Disease_Data['Data']
5
ax.bar(Disease_Data_Loc ,Disease_Data_Value )
6
plt.show()
7
Please help, thank you
Advertisement
Answer
- Just plot the dataframe
JavaScript
1
14
14
1
import pandas as pd
2
import matplotlib.pyplot as plt
3
4
data = {'Location': ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado'],
5
'Data': [30.675, 27.3917, 25.825, 29.2, 29.116999999999997, 35.65]}
6
7
df = pd.DataFrame(data)
8
9
# plot
10
df.plot('Location', 'Data', kind='bar', legend=False)
11
plt.ylabel('Cases')
12
plt.title('Cases of Disease by State')
13
plt.show()
14