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: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:
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!