pandas时间序列及重采样

现有数据24万条,根据月份统计来电次数

  • 读取数据。 其中有一些缺失数据,选取计数结果不选这一列就行
import pandas as pd

df = pd.read_csv("911.csv")
df["timeStamp"] = pd.to_datetime(df["timeStamp"])
df.info()

在这里插入图片描述

  • 将时间戳设置为索引
df.set_index("timeStamp", inplace=True)
df

在这里插入图片描述

  • 根据月份重采样
# 根据月份 重采样 计算来电次数
df = df.resample("M").count()
df

在这里插入图片描述

  • 做图
from matplotlib import pyplot as plt

font = {
    'family' : 'simhei',
    'weight' : 'bold',
    'size'   : '12'
}

plt.rc('font', **font)

# 取出完好失的列数据
data = df["lat"]

# 格式化时间
x_label = [date.strftime("%Y-%m") for date in data.index]
x = range(len(x_label))

y = data.values

plt.figure(figsize=(14, 8),dpi=80)

plt.bar(x, y)

plt.xticks(x, x_label, rotation=45)

plt.title("每个月来电次数状况")

for i,j in zip(x, y):
    plt.text(i-0.4, j+100, j, fontsize=10)

plt.show()

在这里插入图片描述