Skip to content Skip to sidebar Skip to footer

Count Number Of Rows For A Timestamp

I am working on the dataset https://pastebin.com/PEFUspiU I have to group it and count how many requests are there for a particular period of time and then it will be easy to draw

Solution 1:

If you want to count them for each hour, instead of value_count() you can group them and then count, for that, make sure that your timestamp is pandas datetime:

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

Solution 2:

Reading File:

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

Post a Comment for "Count Number Of Rows For A Timestamp"