一、代码示例
import pandas as pd
from pandas import Series,DataFrame
from pandasql import sqldf
import matplotlib.pyplot
idInfos = 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中也可以看到变量的值
文章介绍了如何使用Python的pandas库读取Excel数据,通过自定义的SQL查询对数据进行操作,最后使用matplotlib绘制条形图,展示了数据分组和计数的结果。
1万+

被折叠的 条评论
为什么被折叠?



