一、代码示例
import pandas as pd
from pandas import Series,DataFrame
from pandasql import sqldf
import matplotlib.pyplotidInfos = DataFrame(pd.read_excel('home_data.xlsx'))print(idInfos.head(2))print(idInfos.dtypes)# print(idInfos[:]['姓名'])
# 自定义一个函数sqldf,并将所有变量传递给sqldf函数
pysqldf = lambda sql: sqldf(sql, globals())
sql = "select idxs,count(*) as cnt from (select 姓名 as name,SUBSTRING(公民身份证号,1,10) as idxs from idInfos)t1 group by idxs having cnt > 5 order by idxs desc"
# print (pysqldf(sql).head(10))result = pysqldf(sql)result.plot(x = "idxs",y = "cnt",kind = "bar")
相当于将excel中的数据,读取到idInfos表中,然后通过sql,对idInfos表进行操作并绘制图像;
效果如下:
在IDE中也可以看到变量的值