Reputation: 441
I'm doing an exercise in DataQuest where I'm trying to graph a set of data that I got from SQL.
It gives me the error:
AttributeError: 'str' object has no attribute 'values'
How do I have name
on the x-axis and Pop_Density
on the y-axis? I understand that Python can't graph the name but how do I reference the name along with the number? Do I have to transform the DataFrame into a dictionary first? The line with the problem is commented below.
import pandas as pd
import sqlite3
conn = sqlite3.connect("factbook.db")
q7 = '''
SELECT name, CAST(population as float)/CAST(area_land as float) AS Pop_Density
FROM facts
ORDER BY Pop_Density DESC
LIMIT 20
'''
density = pd.read_sql_query(q7, conn)
print(density)
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
bar_heights = density['name'].iloc[0].values # <-- line with problem.
bar_positions = arange(5) + 0.75
tick_positions = range(1, 20)
ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(num_cols, rotation=90)
ax.set_xlabel("Country")
ax.set_ylabel("Population Density")
ax.set_title("Countries With The Highest Population Density")
Upvotes: 2
Views: 61
Reputation: 1637
bar_heights = density['Pop_Density'].values
bar_positions = np.arange(len(bar_heights)) + 0.75
tick_positions = range(1, len(bar_heights) + 1)
ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(density['name'], rotation=90)
Upvotes: 2