【无标题】

文章通过Pandas库处理911事件的数据,将时间字符串转换为时间类型并设置为索引,然后按类别分组统计每月事件数量并进行可视化展示。同时,文章展示了如何修改时间结构以及处理和展示PM2.5数据的时间变化,特别是针对北京地区的7天平均值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值