上期文章

python数据分析(六)——pandas时间序列


pandas综合案例


PM2.5案例

periodindex时间段

periods = pd.PeriodIndex(year=data["year"],month=data["month"],day=data["day"],hour=data["hour"],freq="H")

# 降采样
data = df.set_index(periods).resample("10D").mean()

五个城市pm2.5随时间变化的情况

import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv("BeijingPM20100101_20151231.csv")

# pd.set_option('display.max_columns', None)
# print(df.head(5))

# 把分开的时间字符串通过periodindex的方法转化为pandas的时间类型
period = pd.PeriodIndex(year=df["year"], month=df["month"], day=df["day"], hour=df["hour"], freq="H")
df["datetime"] = period

# 把datetime设置为索引
df.set_index("datetime", inplace=True)

# 降采样
df = df.resample("7D").mean()

# 处理缺失数据,删除缺失数据
data = df["PM_US Post"].dropna()

# 画图
_x = data.index
_y = data.values

plt.figure(figsize=(20, 8), dpi=80)
plt.plot(range(len(_x)), _y)
plt.xticks(range(len(_x))[::10], _x[::10], rotation=45)

plt.show()
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐