pandas 实战 连接mysql 统计公众号情况

栏目: 数据库 · 发布时间: 7年前

1. 连接mysql,使用 read_sql

import pymysql
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

connect = pymysql.connect(
            host = '127.0.0.1',
            db = 'wxarticle',
            user = 'root',
            passwd = '',
            charset = 'utf8',
            use_unicode = True
        )
cursor = connect.cursor()

select_sql = "select date,code,articleClicksCount,articleCount,ad_count from wxcode"
df = pd.read_sql(select_sql, con=connect)

2. groupby sum

dfDate = df.groupby('date').sum()

3. 画折线图

x = dfDate.index
y = dfDate['articleClicksCount'].values
plt.figure(figsize=(8,4)) #创建绘图对象
plt.plot(x,y,"b",linewidth=1)   #在当前绘图对象绘图(X轴,Y轴,蓝色,线宽度)
plt.xlabel("Date)") #X轴标签
plt.ylabel("Article Total Read Count")  #Y轴标签
plt.title("Total Read Count") #图标题
plt.show()  #显示图

4. np sum 统计总和

qX = ['Q1', 'Q2']
qY = [np.sum(dfDate['articleClicksCount'].values[0:3]), np.sum(dfDate['articleClicksCount'].values[3:])]
plt.figure(figsize=(8,4)) #创建绘图对象
plt.plot(qX,qY,"b",linewidth=1)   #在当前绘图对象绘图(X轴,Y轴,蓝色,线宽度)
plt.xlabel("Date)") #X轴标签
plt.ylabel("Article Total Read Count")  #Y轴标签
plt.title("Total Read Count Q1, Q2") #图标题
plt.show()  #显示图

5. 过滤行

dfAdDate = df.loc[df['ad_count'] == 1]
4758

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Once Upon an Algorithm

Once Upon an Algorithm

Martin Erwig / MIT Press / 2017-9-8 / GBP 22.95

How Hansel and Gretel, Sherlock Holmes, the movie Groundhog Day, Harry Potter, and other familiar stories illustrate the concepts of computing. Picture a computer scientist, staring at a screen and......一起来看看 《Once Upon an Algorithm》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具