#911数据中不同月份不同类型的电话的次数的变化情况
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
file_path = './911.csv'
data = pd.read_csv(file_path)
# print(data.head(3))
# print(data.info())
#将电话类别筛出来
temp_list = data['title'].str.split(': ').tolist()
cata_list = [i[0] for i in temp_list]
# print(cata_list)
#将类别补充到数据中
data['cata'] = pd.DataFrame(np.array(cata_list))
# print(data.head(3))
# print(data.info())
#将日期更新成dataForme中的时间格式,设置索引方便分组
data['timeStamp'] = pd.to_datetime(data['timeStamp'])
data.set_index('timeStamp', inplace=True)
print(data.head(3))
plt.figure(figsize=(20, 8), dpi=80)
for group_name, group_data in data.groupby(by='cata'):
#因为之前将时间设置成了索引,所以可以直接通过resample()方法,选择不同的模式进行分组统计
temp_data = group_data.resample('M').count()['title']
_x = temp_data.index
_y = temp_data.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()
# 请绘制出5个城市的PM2.5随时间的变化情况
import pandas as pd
from matplotlib import pyplot as plt
file_path = './PM2.5/BeijingPM20100101_20151231.csv'
data = pd.read_csv(file_path)
# print(data.head())
# print(data.info())
#补全日期形式
data['datetime'] = pd.PeriodIndex(year=data['year'], month=data['month'], day=data['day'], hour=data['hour'], freq='H')
# print(data.head())
print(data.info())
#设置时间为索引
data.set_index('datetime', inplace=True)
print(data.head())
#重采样降低数据密集程度
data = data.resample('7D').mean()
data_US = data['PM_US Post']
print(data_US)
plt.figure(figsize=(20, 8), dpi=80)
_x = data_US.index
_x = [i.strftime('%Y%m%d') for i in _x]
_y = data_US.values
plt.plot(range(len(_x)), _y)
plt.xticks(range(0, len(_x), 10), _x[::10], rotation=45)
plt.show()