• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Creation of a time-only frequency in Python

Resolved: Creation of a time-only frequency in Python

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m trying to generate a frequency DataFrame in Jupyter/Python of HH:MM:SS elements of a datetime column.
Asside from itterating through all HH:MM:SS combinations and counting them (I need to include 0 values), is there a function in Python that can do it for me?
.value_counts() creates what I need, however, 0 values are not included.
Many thanks, in advance, for your assistance ๐Ÿ™‚
EDIT:
Example Data:
TransactionID DateTime Date Time
012sad9j20j 01/01/22 04:23:32 01/01/22 04:23:32
938hfd82dj2 07/04/22 23:12:59 07/04/22 23:12:59
s9j20jd902j 18/05/22 13:44:19 18/05/22 13:44:19

Expected to generate a dataframe containing:
Time Count
04:23:31 0
04:23:32 1
04:23:33 0

Answer:

Here’s a solution:
This basically creates an additional DataFrame times based on the original data (assuming you don’t even have a Time and Date column, but if you do, you can of course use those) – times takes all the times from df and then groups and counts them.
However, that is missing the times which don’t occur in df, so time_counts is constructed by generated all possible times and either selecting the count from times or 0 if it doesn’t exist in times.
Result:
User @riley’s suggestion seems to point at a nicer solution, something like:
But that seems to have the same problem as you originally stated, even though the dtype of the 'Time' column is category. Perhaps someone has additional suggestions to make that work.

If you have better answer, please add a comment about this, thank you!

pandas python time-series
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: What is a difference between TextStyle.FULL and TextStyle.FULL_STANDALONE

22/03/2023

Resolved: How to access custom object in javascript module from another module?

22/03/2023

Resolved: Is it possible to return the return from a lambda when using Api gateway with eventbridge?

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.