从 SQLAlchemy 结果到图表
我们还可以借助 pandas 和 Matplotlib 将数据可视化成图形。请记住,数据可视化对于探索性数据分析和传达您的数据都至关重要!
本练习是课程的一部分
Python 中的数据库入门
练习说明
- 将
matplotlib.pyplot以别名plt导入。 - 使用提供的
results调用pd.DataFrame()创建 DataFramedf。 - 将 DataFrame 的列
df.columns设置为首个结果对象的列名results[0].keys()。 - 打印 DataFrame
df。 - 在
df上使用plot.bar()方法创建结果的柱状图。 - 使用
plt.show()显示图表。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import pyplot as plt from matplotlib
# Create a DataFrame from the results: df
df = ____
# Set Column names
df.columns = ____
# Print the DataFrame
# Plot the DataFrame