1 统计不同月份不同类型电话次数
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df = pd.read_csv("./911.csv")
#获取分类情况
temp_list = df["title"].str.split(":").tolist()
cate_list = list(set([i[0] for i in temp_list ]))
#df里面添加1列,按照这1列进行分类
df["cate"] = pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
df.groupby(by="cate").count()["title"]
#构造全为0的数组
zeros_df = np.DataFrame((np.zeros((df.shape[0],len(cate_list))),columns=cate_list)
#赋值1
for cate in cate_list:
zero_df[cate][df["title"].str.contains(cate)] = 1
#赋值2(慢,不建议)
for i in range(df.shape[0]):
zeros_df.loc[i,temp_list[i][0]] = 1
sum_set = zeros_df.sum(axis=0)
2 pandas时间序列
pd.date_range(start=,end=,periods=,freq=)
import pandas as pd
#每隔1天
pd.data_range(start="20171230",end="20180131",freq="D")
#每隔10天
pd.data_range(start="20171230",end="20180131",freq="10D")
#生成10个
pd.data_range(start="20171230",periods,freq="D")
#月份
freq="M"
freq="H"
pandas重采样 resample
将时间序列从一个频率转化为另一个频率进行处理的过程。
import pandas as pd
import numpy as np
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)
#不同月份的电话次数
count_by_month = df.resample("M").count()["title"]
print(count_by_month)
#画图
_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)
plt.xticks(range(len(_x)),_x,rotation=45)
plt.show()
#不同月份不同类型的电话次数
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
def plot_img(df,label):
count_by_month = df.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=label)
plt.xticks(range(len(_x)),_x,rotation=45)
#把时间字符串转为时间类型设置为索引
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 = list(set([i[0] for i in temp_list ]))
#df里面添加1列,按照这1列进行分类
df["cate"] = pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
df.groupby(by="cate").count()["title"]
#进行分组
for group_name,group_data in df.group(by="cate"):
plot_img(group_data,group_name)
plt.legend(loc="best")
plt.show()
3 5个城市,PM2.5随时间变化
import pandas
from matplotlib import pyplot as plt
file_path = './PM2.5/BeijingPM20100101_20151231'
df = pd.read_csv(file_path)
#把分开的时间字符串通过periodIndex方法转化为pandas时间类型
period = pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
df["datetime"] = period
#把datetime设置为索引
df.set_index("datetime",inplace=True)
#进行降采样
df.resample("7D").mean()
#处理缺失数据,删除
data = df["PM_US Post"].dropna()
#画图
_x = data.index
_x = [i.strftime(%Y%m%d)for i in _x]
_y = values
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(_x)),_y)
plt.xticks(range(0,len(_x),20),list(_x)[::20],rotation=45)
plt.show()