如何从 python 中的 sqldf 制作直方图
How to make a Histogram from a sqldf in python
我在 iPython notebook
中写了以下 SQL 查询
q = """SELECT *
FROM Northeast ORDER BY counted desc LIMIT 10"""
display(sqldf(q))
df = pysqldf(q)
plt.bar(df)
plt.show()
我试图制作它的直方图,但我不断收到关于它需要高等的错误。我以不同的方式尝试得到不同的错误。是否可以根据 sgldf 的结果制作一个直方图,其中城市为 x 轴,人口为 y 轴?
以下脚本应该有助于您尝试执行的操作:
import pandas as pd
from pysqldf import SQLDF
import matplotlib.pyplot as plt
##Set up the sqldf function to query from the globals() variables
sqldf = SQLDF(globals())
##Since you're using ipython notebooks, put plots in line with this: %matplotlib inline
##Get some data from Wikipedia: Gets all sorts of information on cities in the Northeast
cities = pd.read_html('https://en.wikipedia.org/wiki/Northeastern_United_States',header = 0)
Northeast = cities[1] ##This where the top 10 cities are
Northeast.head(2) ## Take a look
Northeast.columns = ['Rank', 'MetropolitanArea', 'States', 'Population'] ##Change the columns to look better
##Run your query: This gets city name and Population
df = sqldf.execute('SELECT MetropolitanArea, Population FROM Northeast ORDER BY Population desc LIMIT 10;')
##This get the bar plot
df.plot(x='MetropolitanArea',y='Population',kind='bar')
plt.show()
谢谢!
我在 iPython notebook
中写了以下 SQL 查询q = """SELECT *
FROM Northeast ORDER BY counted desc LIMIT 10"""
display(sqldf(q))
df = pysqldf(q)
plt.bar(df)
plt.show()
我试图制作它的直方图,但我不断收到关于它需要高等的错误。我以不同的方式尝试得到不同的错误。是否可以根据 sgldf 的结果制作一个直方图,其中城市为 x 轴,人口为 y 轴?
以下脚本应该有助于您尝试执行的操作:
import pandas as pd
from pysqldf import SQLDF
import matplotlib.pyplot as plt
##Set up the sqldf function to query from the globals() variables
sqldf = SQLDF(globals())
##Since you're using ipython notebooks, put plots in line with this: %matplotlib inline
##Get some data from Wikipedia: Gets all sorts of information on cities in the Northeast
cities = pd.read_html('https://en.wikipedia.org/wiki/Northeastern_United_States',header = 0)
Northeast = cities[1] ##This where the top 10 cities are
Northeast.head(2) ## Take a look
Northeast.columns = ['Rank', 'MetropolitanArea', 'States', 'Population'] ##Change the columns to look better
##Run your query: This gets city name and Population
df = sqldf.execute('SELECT MetropolitanArea, Population FROM Northeast ORDER BY Population desc LIMIT 10;')
##This get the bar plot
df.plot(x='MetropolitanArea',y='Population',kind='bar')
plt.show()
谢谢!