1:不同月份不同类型911事件的发生情况
#coding=utf-8
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
date_path='./911.csv'
#求不同月份不同类别的数量
#把时间字符串转化为时间类型,设置为索引
df=pd.read_csv(date_path)
#print(df.info())
df['timeStamp']=pd.to_datetime(df['timeStamp'])
#添加列表示分类
temp_list=df['title'].str.split(':').tolist()
cate_list=[i[0]for i in temp_list]
df['cates']=pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
df.set_index('timeStamp',inplace=True)
plt.figure(figsize=(20,8),dpi=80)
#print(np.array(cate_list))
#分组
for group_name,group_date in df.groupby(by='cates'):
#对不同的分类进行绘图
count_by_month = group_date.resample('M').count()['title']
# 画图
_x = count_by_month.index
_y = count_by_month.values
_x = [i.strftime('%Y%m%d') for i in _x]
plt.plot(range(len(_x)), _y, label=group_name)
plt.xticks(range(len(_x)),_x,rotation=45)
plt.legend(loc='best')
plt.show()
2:时间结构的修改
pd.PeriodIndex(year=df['year'],month=df['month'],day=df['day'],hour=df['hour'],freq='H')
#绘制出5个城市pm2.5随时间的变化情况
#处理缺失数据
#处理缺失数据,删除缺失数据 date=df['PM_US Post'].dropna()
#处理数据的结构
_x=data.index _x=[i.strftime('%Y%m%d') for i in _x ]
2:画出PM2.5不同地区不同时间的PM值的变化
#coding=utf-8
import pandas as pd
from matplotlib import pyplot as plt
pd.set_option('display.max_columns',200)
file_path='./PM2.5/BeijingPM20100101_20151231.csv'
df=pd.read_csv(file_path)
#print(df.head())
#print(df.info())
period=pd.PeriodIndex(year=df['year'],month=df['month'],day=df['day'],hour=df['hour'],freq='H')
# print(period)
# print(type(period))
df['datetime']=period
#print(df.head(10))
#把datetime设置为索引
df.set_index('datetime',inplace=True)
df=df.resample('7D').mean()
#print(df.head())
data=df['PM_US Post']
data_china=df['HUMI']
#print(data_china.head())
#print(df)
#处理缺失数据,删除缺失数据
#data=df['PM_US Post'].dropna()
_x=data.index
_x=[i.strftime('%Y%m%d') for i in _x ]
_x_china=[i.strftime('%Y%m%d')for i in data_china.index]
_y=data.values
_y_china=data_china.values
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(_x)),_y,label='US_POST')
plt.plot(range(len(_x)),_y_china,label='CN_POST')
plt.xticks(range(0,len(_x),20),list(_x)[::20],rotation=45)
plt.legend(loc='best')
plt.show()