python使用pandas实现数据分割实例代码

作者:llwang_10 时间:2021-07-02 11:09:19 

本文研究的主要是Python编程通过pandas将数据分割成时间跨度相等的数据块的相关内容,具体如下。

先上数据,有如下dataframe格式的数据,列名分别为date、ip,我需要统计每5s内出现的ip,以及这些ip出现的频数。


ip   date
0 127.0.0.21 15/Jul/2017:18:22:16
1 127.0.0.13 15/Jul/2017:18:22:16
2 127.0.0.11 15/Jul/2017:18:22:17
3 127.0.0.11 15/Jul/2017:18:22:20
4 127.0.0.21 15/Jul/2017:18:22:21
5 127.0.0.13 15/Jul/2017:18:22:22
6 127.0.0.14 15/Jul/2017:18:26:36
7 127.0.0.16 15/Jul/2017:18:32:15
8 127.0.0.11 15/Jul/2017:18:36:03

在网上找了很久但是没看到python的相关答案,但在stackoverflow找到了R语言的解法,有兴趣可以看看。

受它的启发,我用不太优雅的方式实现了我的需求,有更好解决方法的请不吝赐教:

step1: 将数据中日期格式变为标准格式


#date_ip为我的dataframe数据
date_ip['date'] = pd.to_datetime(date_ip['date'], format='%d/%b/%Y:%H:%M:%S')

step2: 将数据的开始时间、结束时间,按5s分割(由于时间段可能不是恰好是5s的倍数,为避免最后一个时间丢失,因此在最后加上5s)


frequency = 5
time_range = pd.date_range(date_ip['date'][0],
   date_ip['date'][date_ip.shape[0]-1]
   +frequency*Second(), freq='%sS'%frequency)

step3: 将date变为索引


date_ip = date_ip.set_index('date')

step4: 对每个时间段内的数据进行频数计算(由于通过标签切片时会包含头、尾数据,为避免重复计算,因此在尾部减1s)


for i in xrange(0,len(time_range)-1):
print get_frequency(date_ip.loc[time_range[i]:time_range[i+1]-1*Second()])

完整的代码


import pandas as pd
from pandas.tseries.offsets import Second
def get_frequency(date_ip):
ip_frequency = {}
for i in xrange(0,date_ip.shape[0]):
ip_frequency[date_ip['ip'][i]] = ip_frequency.get(date_ip['ip'][i], 0) + 1
return ip_frequency,date_ip.shape[0]

if __name__ == '__main__':
date_ip['date'] = pd.to_datetime(date_ip['date'], format='%d/%b/%Y:%H:%M:%S')

frequency = 5
time_range = pd.date_range(date_ip['date'][0], date_ip['date'][date_ip.shape[0]-1]
   +frequency*Second(), freq='%sS'%frequency)
date_ip = date_ip.set_index('date')
for i in xrange(0, len(time_range) - 1):
print get_frequency(date_ip.loc[time_range[i]:time_range[i + 1]-1*Second()])

文章开头数据运行结果:


({'127.0.0.21' : 1, '127.0.0.13' : 1, '127.0.0.11' : 2}, 4)
({'127.0.0.21': 1, '127.0.0.13': 1}, 2)
({'127.0.0.14': 1}, 1)
({'127.0.0.16': 1}, 1)
({'127.0.0.11': 1}, 1)

总结

来源:http://blog.csdn.net/llwang_10/article/details/78590333

标签:python,pandas,数据
0
投稿

猜你喜欢

  • asp超强的Server Application Error 的解决方法

    2008-11-13 13:04:00
  • 如何判断SQL语句是否执行了?

    2010-01-12 20:03:00
  • 如何使用PyCharm引入需要使用的包的方法

    2021-12-22 07:34:56
  • python PyTorch预训练示例

    2022-07-06 18:24:02
  • Python OpenCV一个窗口中显示多幅图像

    2023-12-09 19:38:04
  • Python选课系统开发程序

    2023-07-21 00:25:03
  • Python中的zipfile模块使用详解

    2023-02-26 22:44:37
  • C#调用python.exe使用arcpy方式

    2021-03-30 05:41:13
  • asp中Adodb.Stream 的使用说明

    2007-09-11 13:53:00
  • python交互模式下输入换行/输入多行命令的方法

    2022-06-08 07:18:37
  • asp select下拉菜单选择图标并实时显示

    2011-04-03 10:33:00
  • 订单转化率之回访确认

    2009-08-24 12:40:00
  • python绘制双柱形图代码实例

    2022-02-28 19:42:27
  • 如何利用Python模拟GitHub登录详解

    2023-11-18 11:08:05
  • Python Flask框架模板操作实例分析

    2022-09-21 02:56:23
  • python 装饰器的基本使用

    2021-04-01 07:12:50
  • Python 中 -m 的典型用法、原理解析与发展演变

    2023-07-09 17:11:40
  • Python实现对中文文本分段分句

    2022-09-16 18:16:50
  • 网页设计布局原则

    2010-04-20 17:18:00
  • 用css+Javascript实现扫描线效果图片

    2007-11-08 19:12:00
  • asp之家 网络编程 m.aspxhome.com