2016-08-11 3 views
0

Я написал следующий SQL запрос в IPython ноутбукеКак сделать гистограмму из sqldf в питоне

q = """SELECT * 
FROM Northeast ORDER BY counted desc LIMIT 10""" 
display(sqldf(q)) 

df = pysqldf(q) 

plt.bar(df) 
plt.show() 

Я пытался сделать гистограмму это, но я получаю ошибки об этом нужен высоких и т.д. Я пытаюсь по-разному получить разные ошибки. Можно ли вывести из результата sgldf гистограмму бара с городом как x ax и популяцию как y ax?

+0

просто используйте 'hist', а не' bar' – Anonymous

+0

также в чем разница между 'pysqldf (q)' и 'sqldf (q)'? Разве это не должно быть 'sqldf (q)' на всем протяжении? – Abdou

+0

По умолчанию Я получаю этот eerror ТипError: len() объекта, не связанного с неизвестностью – Vedad

ответ

1

Следующий скрипт должен помочь с тем, что вы пытаетесь сделать:

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() 

Спасибо!

Смежные вопросы