最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用pandas實(shí)現(xiàn)數(shù)據(jù)分割實(shí)例代碼

 更新時(shí)間:2018年01月25日 09:48:36   作者:llwang_10  
這篇文章主要介紹了python使用pandas實(shí)現(xiàn)數(shù)據(jù)分割實(shí)例代碼,介紹了使用pandas實(shí)現(xiàn)對(duì)dataframe格式的數(shù)據(jù)分割成時(shí)間跨度相等的數(shù)據(jù)塊,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是Python編程通過(guò)pandas將數(shù)據(jù)分割成時(shí)間跨度相等的數(shù)據(jù)塊的相關(guān)內(nèi)容,具體如下。

先上數(shù)據(jù),有如下dataframe格式的數(shù)據(jù),列名分別為date、ip,我需要統(tǒng)計(jì)每5s內(nèi)出現(xiàn)的ip,以及這些ip出現(xiàn)的頻數(shù)。

 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

在網(wǎng)上找了很久但是沒(méi)看到python的相關(guān)答案,但在stackoverflow找到了R語(yǔ)言的解法,有興趣可以看看。

受它的啟發(fā),我用不太優(yōu)雅的方式實(shí)現(xiàn)了我的需求,有更好解決方法的請(qǐng)不吝賜教:

step1: 將數(shù)據(jù)中日期格式變?yōu)闃?biāo)準(zhǔn)格式

#date_ip為我的dataframe數(shù)據(jù)
date_ip['date'] = pd.to_datetime(date_ip['date'], format='%d/%b/%Y:%H:%M:%S')

step2: 將數(shù)據(jù)的開(kāi)始時(shí)間、結(jié)束時(shí)間,按5s分割(由于時(shí)間段可能不是恰好是5s的倍數(shù),為避免最后一個(gè)時(shí)間丟失,因此在最后加上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變?yōu)樗饕?/h2>
date_ip = date_ip.set_index('date')

step4: 對(duì)每個(gè)時(shí)間段內(nèi)的數(shù)據(jù)進(jìn)行頻數(shù)計(jì)算(由于通過(guò)標(biāo)簽切片時(shí)會(huì)包含頭、尾數(shù)據(jù),為避免重復(fù)計(jì)算,因此在尾部減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()])

文章開(kāi)頭數(shù)據(jù)運(yùn)行結(jié)果:

({'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)

總結(jié)

以上就是本文關(guān)于python使用pandas實(shí)現(xiàn)數(shù)據(jù)分割實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

游戏| 广丰县| 宁安市| 张掖市| 浦东新区| 宁晋县| 阳曲县| 唐海县| 区。| 德令哈市| 兴文县| 涟水县| 布尔津县| 特克斯县| 马鞍山市| 肃北| 远安县| 额尔古纳市| 岳池县| 清苑县| 仙游县| 安岳县| 策勒县| 洞头县| 襄樊市| 肇庆市| 额敏县| 庆城县| 同心县| 迭部县| 都安| 互助| 喀什市| 玉树县| 延川县| 信宜市| 宁波市| 搜索| 岗巴县| 唐山市| 平定县|