数据集:
代码:
# -*- coding: utf-8 -*-
# @File : pandas_dataframe_periodIndex_demo.py
# @Date : 2020-01-07 15:59
# @Author : admin
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np;
df=pd.read_csv("../../data/BeijingPM20100101_20151231.csv");
#df=df.head(1000);
print(df.head(5))
print(df.info())
print("=================================================将离散型的时间字段,使用preriodinde封装成pandas的时间序列=========")
d=pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
#print(d)
#新增一列
df["time"]=d;
#设置为索引
df.set_index("time",inplace=True);
print(df)
#重采样,降维,以7天显示一下
df = df.resample("7D").mean()
print(df.head())
#处理缺失数据
us_data=df["PM_US Post"].dropna();
cn_data=df["PM_Nongzhanguan"].dropna();
#us_data=df["PM_US Post"]
#cn_data=df["PM_Nongzhanguan"]
x_us=us_data.index;
x_us=[m.strftime("%Y%m%d") for m in x_us]
y_us=us_data.values;
x_cn=cn_data.index;
x_cn=[m.strftime("%Y%m%d") for m in x_cn];
y_cn=cn_data.values;
print(len(x_us),len(x_cn))
print("###################################################画图展示###########################")
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(x_us)),y_us,label="us",alpha=0.7);
plt.plot(range(len(x_cn)),y_cn,label="cn",alpha=0.7);
#plt.xticks(range(len(x_us)),x_us);
plt.xticks(range(0,len(x_us),10),list(x_us)[::10],rotation=45);
plt.legend(loc="best")
plt.show();
展示:
如果x轴显示的时间太多,我们可以设定步长显示:
plt.xticks(range(0,len(x_us),10),list(x_us)[::10],rotation=45);
从图上可看到:10年到13年中国的数据是缺失的,居然有数据显示,主要是中国和美国两个城市的数据长度不一致造成,
313 155
假设,不过滤nan的数据:
us_data=df["PM_US Post"].dropna(); cn_data=df["PM_Nongzhanguan"].dropna();
改为:
us_data=df["PM_US Post"] cn_data=df["PM_Nongzhanguan"]
再执行: