Kashan
Kashan

Reputation: 348

Extract Time from DateTime dataframe column and Plot hourly time with lineplot

As I have a column in DateTime format and want to plot a graph which will show hourly time intervals to find out hours in which users are most active. However, I'm a bit confused with how to plot seaborn lineplot with extracted time and hourly time interval on x-axis. Any help will be very much appreciated.

Datetime Column:

 2019-06-16 09:50:32.716000
 2019-06-16 09:50:32.716001
 2019-06-16 09:50:35.374002
 2019-06-16 09:50:36.032003
 2019-06-16 06:41:09.959000
 2019-06-16 07:04:59.880000
 2019-06-16 07:05:09.925000
 2019-06-16 07:05:18.680002
 2019-06-16 07:05:18.676003
 2019-06-16 07:06:04.646004
 2019-06-16 07:06:04.646005
 2019-06-16 07:06:25.354006
 2019-06-16 07:06:25.353007
 ....
 2019-06-16 07:06:28.464008
 2019-06-16 07:06:28.464009
 2019-06-16 07:06:37.777010
 2019-06-16 07:06:37.942011
 2019-06-16 07:07:05.512012
 2019-06-16 07:07:13.342013
 2019-06-16 05:15:38.923000
 2019-06-16 05:15:45.772001
 2019-06-16 05:15:51.937000
 2019-06-16 05:15:57.064003
 2019-06-16 05:15:57.064004
 2019-06-16 05:16:07.163005
 2019-06-16 05:16:07.162006

Code so far:

sns.lineplot(data=dfdays[0]['event_timestamp'].dt.hour, color="coral", 
label="line")

Upvotes: 1

Views: 549

Answers (1)

Kashan
Kashan

Reputation: 348

After quite a bit of searching, I found the answer. Maybe this can help others.

Extracting time from dataframe:

minutes=[]
hours=[]
time=[]
minutes=dfdays[0].event_timestamp.dt.minute
hours=dfdays[0].event_timestamp.dt.hour
for i in range(len(dfdays[0])):
 x=float(str(hour[i])+'.'+str(minutes[i]))
 time.append(x)

Counting the entries from array:

 x = np.array(time)
 unique, counts = np.unique(x, return_counts=True)

Ploting with Seaborn:

sns.lineplot(unique,counts,color="coral", label="line")
plt.xlabel('Hours')
plt.ylabel('Users count')
plt.tight_layout()
plt.show()

Output:enter image description here

Upvotes: 3

Related Questions