计算时间戳的行数

2024-09-30 08:32:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理数据集

https://pastebin.com/PEFUspiU

我必须对它进行分组并计算在特定时间段内有多少个请求,然后就可以很容易地绘制出时间与请求数的图表。

举个例子

**timestamp - number of request**

21-06-2016 09:00:00 - 2

21-06-2016 10:00:00 - 1

21-06-2016 11:00:00 - 5

我怎么能算出这个数?在

谢谢

我尝试使用data['timestamp'].value_counts()但出现错误:

^{pr2}$

Tags: of数据httpscomnumberdatarequest图表
2条回答

如果您想每小时对它们进行计数,则可以对它们进行分组,然后进行计数,为此,请确保您的时间戳是pandas datetime:

df['timestamp'] = pd.to_datetime(df['timestamp'])
df.groupby(pd.Grouper(key='timestamp', freq="1H")).count()

正在读取文件:

 df = pd.read_csv('/home/local/sayali/Downloads/dataset-server_logs.csv')

[In]:df

              host            timestamp  status   byte
0  192.168.102.100  21-06-2016 09:54:44     200  17811
1  192.168.102.100  21-06-2016 09:54:44     200  21630
2  192.168.100.160  21-06-2016 10:08:08     404   1098
3  192.168.100.160  21-06-2016 11:20:44     200  17811
4  192.168.100.160  21-06-2016 11:20:44     200  21630
5  192.168.102.100  21-06-2016 11:54:44     200  17811
6  192.168.102.100  21-06-2016 11:54:44     200  21630
7  192.168.102.100  21-06-2016 11:54:44     200  21630

ts = pd.DataFrame(df['timestamp'].value_counts()))

ts
Out[15]: 
                     timestamp
2016-06-21 11:54:44          3
2016-06-21 09:54:44          2
2016-06-21 11:20:44          2
2016-06-21 10:08:08          1

#Convert index to datetime format using pd.to_datetime()
ts.index = pd.to_datetime(ts.index)

# PLOT
plt.title('Number of Requests based on timestamp') 
plt.xlabel('Timestamp')
plt.ylabel('Total number of Requests') 
#Change xticks orientation to vertical 
plt.xticks(rotation='vertical')        
plt.plot(ts)

enter image description here

相关问题 更多 >

    热门问题