1. # coding=utf-8
    2. import pandas as pd
    3. import numpy as np
    4. from matplotlib import pyplot as plt
    5. df = pd.read_csv("./911.csv")
    6. df["timeStamp"] = pd.to_datetime(df["timeStamp"])
    7. df.set_index("timeStamp",inplace=True)
    8. #统计出911数据中不同月份电话次数的
    9. count_by_month = df.resample("M").count()["title"]
    10. print(count_by_month)
    11. #画图
    12. _x = count_by_month.index
    13. _y = count_by_month.values
    14. # for i in _x:
    15. # print(dir(i))
    16. # break
    17. _x = [i.strftime("%Y%m%d") for i in _x]
    18. plt.figure(figsize=(20,8),dpi=80)
    19. plt.plot(range(len(_x)),_y)
    20. plt.xticks(range(len(_x)),_x,rotation=45)
    21. plt.show()
    1. # coding=utf-8
    2. #911数据中不同月份不同类型的电话的次数的变化情况
    3. import pandas as pd
    4. import numpy as np
    5. from matplotlib import pyplot as plt
    6. #把时间字符串转为时间类型设置为索引
    7. df = pd.read_csv("./911.csv")
    8. df["timeStamp"] = pd.to_datetime(df["timeStamp"])
    9. #添加列,表示分类
    10. temp_list = df["title"].str.split(": ").tolist()
    11. cate_list = [i[0] for i in temp_list]
    12. # print(np.array(cate_list).reshape((df.shape[0],1)))
    13. df["cate"] = pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
    14. df.set_index("timeStamp",inplace=True)
    15. print(df.head(1))
    16. plt.figure(figsize=(20, 8), dpi=80)
    17. #分组
    18. for group_name,group_data in df.groupby(by="cate"):
    19. #对不同的分类都进行绘图
    20. count_by_month = group_data.resample("M").count()["title"]
    21. # 画图
    22. _x = count_by_month.index
    23. print(_x)
    24. _y = count_by_month.values
    25. _x = [i.strftime("%Y%m%d") for i in _x]
    26. plt.plot(range(len(_x)), _y, label=group_name)
    27. plt.xticks(range(len(_x)), _x, rotation=45)
    28. plt.legend(loc="best")
    29. plt.show()