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"])
print(type(df["timeStamp"]))
df.set_index("timeStamp", inplace=True)
df.head(3)
<class 'pandas.core.series.Series'>
| lat | lng | desc | zip | title | twp | addr | e |
---|
timeStamp | | | | | | | | |
---|
2015-12-10 17:10:52 | 40.297876 | -75.581294 | REINDEER CT & DEAD END; NEW HANOVER; Station ... | 19525.0 | EMS: BACK PAINS/INJURY | NEW HANOVER | REINDEER CT & DEAD END | 1 |
---|
2015-12-10 17:29:21 | 40.258061 | -75.264680 | BRIAR PATH & WHITEMARSH LN; HATFIELD TOWNSHIP... | 19446.0 | EMS: DIABETIC EMERGENCY | HATFIELD TOWNSHIP | BRIAR PATH & WHITEMARSH LN | 1 |
---|
2015-12-10 14:39:21 | 40.121182 | -75.351975 | HAWS AVE; NORRISTOWN; 2015-12-10 @ 14:39:21-St... | 19401.0 | Fire: GAS-ODOR/LEAK | NORRISTOWN | HAWS AVE | 1 |
---|
二、利用df.resample()统计
count_by_month = df.resample("M").count()["title"]
count_by_month.head(3)
timeStamp
2015-12-31 7916
2016-01-31 13096
2016-02-29 11396
Freq: M, Name: title, dtype: int64
三、绘图
_x = count_by_month.index
_y = count_by_month.values
print(type(_x[0]))
_x = [i.strftime("%Y%m%d") for i in _x]
print(_x[0])
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x, rotation=45)
plt.show()
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
20151231