1.Pandas时间序列序列
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df=pd.read_csv('./911.csv')
#print(df.head(10))
#print(df.info())
##获取分类情况
temp_list=df['title'].str.split(':').to_list()
cate_list=list(set([i[0] for i in temp_list ]))
print(cate_list)
##构造全为0的数组
zeros_df=pd.DataFrame(np.zeros((df.shape[0],len(cate_list))),columns=cate_list)
##赋值 快
for cate in cate_list:
zeros_df[cate][df['title'].str.contains(cate)]=1
# print(zeros_df)
# break
# ##赋值方法2__慢
# for i in range(df.shape[0]):
# zeros_df.loc[i,temp_list[i][0]]=1
# print(zeros_df)
sum_ret=zeros_df.sum(axis=0)
print(sum_ret)
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df=pd.read_csv('./911.csv')
#print(df.head(10))
#print(df.info())
##获取分类情况
temp_list=df['title'].str.split(':').to_list()
cate_list=[i[0] for i in temp_list ]
cate_df=pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)),columns=['cate'])
print(cate_df)
df['cate']=cate_df ##添加一列
##或者
##df['cate']=pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)),columns=['cate'])
print(df.head(5))
print(df.groupby(by='cate').count()['title'])
##不同月份紧急电话的次数
解决上述问题可以用Pandas时间序列完成
参数见笔记
import pandas as pd t1=pd.date_range(start="20171230",end='20180131',freq='D') print(t1)
DatetimeIndex([‘2017-12-30’, ‘2017-12-31’, ‘2018-01-01’, ‘2018-01-02’,
‘2018-01-03’, ‘2018-01-04’, ‘2018-01-05’, ‘2018-01-06’,
‘2018-01-07’, ‘2018-01-08’, ‘2018-01-09’, ‘2018-01-10’,
‘2018-01-11’, ‘2018-01-12’, ‘2018-01-13’, ‘2018-01-14’,
‘2018-01-15’, ‘2018-01-16’, ‘2018-01-17’, ‘2018-01-18’,
‘2018-01-19’, ‘2018-01-20’, ‘2018-01-21’, ‘2018-01-22’,
‘2018-01-23’, ‘2018-01-24’, ‘2018-01-25’, ‘2018-01-26’,
‘2018-01-27’, ‘2018-01-28’, ‘2018-01-29’, ‘2018-01-30’,
‘2018-01-31’],
dtype=‘datetime64[ns]’, freq=‘D’)
Process finished with exit code 0
D为天,10D 10 天 M 月
在DataFrame中使用时间序列
import time
import pandas as pd
from matplotlib import pyplot as plt
df=pd.read_csv('./911.csv')
df['timeStamp']=pd.to_datetime(df['timeStamp'])
df.set_index('timeStamp',inplace=True)
print(df.head())
count_by_month=df.resample("M").count()['title']
print(count_by_month.head)
##
_x=count_by_month.index
_y=count_by_month.values
##查看可用方法
# for i in _x:
# print(dir(i))
# break
_x=[time.strftime('%y%m%d')for i in _x]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x,rotation=45)
plt.show()
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
print('*.* '*50)
##911数据中不同月份不同类型的电话的次数的变化情况
def plot_img(df):
count_by_month=df.resample('M').count()['title']
##把时间字符串转为时间类型设置为索引
df=pd.read_csv('./911.csv')
df['timeStamp']=pd.to_datetime(df['timeStamp'])
#df.set_index('timeStamp',inplace=True)
##添加列,表示分类
temp_list=df['title'].str.split(':').tolist()
cate_list=[i[0] for i in temp_list]
df['cate']=pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
df.set_index('timeStamp',inplace=True)
print(df.head(1))
plt.figure(figsize=(20,8),dpi=80)
##分组
for group_name,group_data in df.groupby(by='cate'):
##对不同分类都进行汇总
count_by_month=group_data.resample('M').count()['title']
##画图
_x=count_by_month.index
print(_x)
_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()
Pandas时间序列之PeriodIndex
之前所学习的DatetimeIndex可以理解为时间戳,那么现在我们要学习的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()
import time
import matplotlib
import pandas as
file_path='./PM2.
df=pd.read_csv(fi
# print(df.head()
# print(df.info()
##把分开的时间字符串通过peri
period=pd.PeriodI
df['datatime']=pe
df['datetime']=pe
##把datetime 设置为索引
df.set_index('dat
print(df.head())
##进行降采样
df=df.resample('7
print(df.head())
##处理缺失数据,删除缺失数据
print(df['PM_US P
data=df['PM_US Po
data_china=df['PM
print(data_china.
##h画图
_x=data.index
_x_china=[i.strft
print(len(_x_chin
_y=data.values
_y_china=data_chi
plt.figure(figsiz
plt.plot(range(le
plt.plot(range(le
plt.xticks(range(
plt.legend(loc='b
plt.show()



