经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
关于pandas时间数据的集成处理
来源:cnblogs  作者:冻雨冷雾  时间:2019/5/28 9:05:54  对本文有异议

工作中遇到的一个问题: 统计各地区新能源汽车的充电时长 数据来源是北理新源的单日全球的运行数据。

这里仅统计北上广重庆四个地区的 数据处理的代码就省略了 需要整理好的是4个dataframe(数据已保存为H5格式) 分别是对应上述4个城市的:

  1. import pandas as pd
  2. from pyecharts import Boxplot,Pie,Page
  3. theme_echart='infographic'
  4.  
  5. location_list=['shanghai','chongqing','guangdong','beijing']
  6. ans_vid={}
  7.  
  8. for i in location_list:
  9. ans_vid[i]=pd.read_hdf(i+'_charging.h5',encoding='gbk')
  10.  
  11. location_list_chinese=['上海','重庆','广东','北京'];
  12. for i in range(len(location_list_chinese)):
  13. ans_vid[location_list_chinese[i]] = ans_vid.pop(location_list[i])

  

例:

这时候我们需要提取其中的时间序列统计所有vid的 充电状态为1的第一个时间和最后一个时间 即为该车的充电时长

代码如下:

  1. page=Page()
  2. for i in location_list_chinese:
  3. ans_vid[i]=ans_vid[i][ans_vid[i]['充电状态']=='1.0']
  4. temp1=ans_vid[i].drop_duplicates(['vid'],keep='last')
  5. temp2=ans_vid[i].drop_duplicates(['vid'],keep='first')
  6. a=temp2['上报时间']
  7. b=temp1['上报时间']
  8. a=a.reset_index()
  9. b=b.reset_index()
  10. a=a.drop(['index'],axis=1)
  11. b=b.drop('index',axis=1)
  12. a['上报时间']=a['上报时间'].astype(str)
  13. a['上报时间']=a['上报时间'].apply(lambda v: v[0:4]+'-'+v[4:6]+'-'+v[6:8]+' '+v[8:10]+':'+v[10:12]+':'+v[12:14])
  14. b['上报时间']=b['上报时间'].astype(str)
  15. b['上报时间']=b['上报时间'].apply(lambda v: v[0:4]+'-'+v[4:6]+'-'+v[6:8]+' '+v[8:10]+':'+v[10:12]+':'+v[12:14])
  16. b['上报时间']=pd.to_datetime(b['上报时间'])
  17. a['上报时间']=pd.to_datetime(a['上报时间'])
  18. temp=b['上报时间']-a['上报时间']
  19. temp=pd.DataFrame(temp)
  20. temp['上报时间']=temp['上报时间'].dt.total_seconds()/3600
  21. temp['充电时长']=temp['上报时间'].astype(str)
  22. temp['充电时长'][temp['上报时间']<=1]='<1h'
  23. temp['充电时长'][(temp['上报时间']>1) & (temp['上报时间']<=4)]='1-4h'
  24. temp['充电时长'][(temp['上报时间']>4) & (temp['上报时间']<=8)]='4-8h'
  25. temp['充电时长'][temp['上报时间']>8]='>8h'
  26. local_charging_time=temp['充电时长'].value_counts()
  27. box=Boxplot(i+'地区充电时长统计')
  28. pie=Pie(i+'地区充电时长统计')
  29. box.use_theme(theme_echart)
  30. pie.use_theme(theme_echart)
  31. # kwargs = dict(name = i,
  32. # x_axis = list(local_charging_time.index),
  33. # y_axis = list(local_charging_time.values),
  34. # is_legend_show=False,
  35. # is_label_show=True
  36. # )
  37. # bar.add(**kwargs)
  38. x=list(local_charging_time.index);
  39. y=list(local_charging_time.values);
  40. pie.add("",x,y,radius=(40,75),
  41. is_label_show=True,legend_orient = 'vertical',
  42. legend_pos = 'left',legend_top='center')
  43. # box画图
  44. y_axis =[]
  45. for j in x:
  46. y_axis.append(list(temp['上报时间'][temp['充电时长']==j]))
  47. y=box.prepare_data(y_axis)
  48. box.add(i+'地区各充电时长分布', x, y,xaxis_name='',
  49. yaxis_name='充电时长[h]',is_legend_show=True,legend_pos='right',is_label_show=True,yaxis_name_gap=45,xaxis_type='category',xaxis_rotate=0)
  50. page.add(pie)
  51. page.add(box)
  52. del box,pie
  53.  
  54. page.render('北上广重地区充电时长统计_v2.html')

  可以看到核心处理程序是pd.to_datetime(a['上报时间']) 转化为时间格式之后 用两列相减得到时间差格式的temp

   temp['上报时间']=temp['上报时间'].dt.total_seconds()/3600 # 此处提取时间差格式的秒数, 再折算成小时

 

结果如下图:

 

 

 一个相似的例子是需要统计这四个地区的充电开始时段的分布(根据电网电价的需求而来)

核心是将连续的时间格式字符Series集成转化成时间格式,即'20190101235502'转化成 2019-01-01 23:55:02

然后调用pd.to_datetime

 

原文链接:http://www.cnblogs.com/techs-wenzhe/p/10931282.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号