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

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

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

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

查看所有标签

猜你喜欢:

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

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具