Pandas: Counting Frequency Of Datetime Objects In A Column
I have a column (from my original data) that I have converted from a string to a datetime-object in Pandas. The column looks like this: 0 2012-01-15 11:10:12 1 2012-01-15 1
Solution 1:
You can first get the date part of the datetime, and then use value_counts
:
s.dt.date.value_counts()
Small example:
In [12]:s=pd.Series(pd.date_range('2012-01-01',freq='11H',periods=6))In [13]:sOut[13]:02012-01-01 00:00:0012012-01-01 11:00:0022012-01-01 22:00:0032012-01-02 09:00:0042012-01-02 20:00:0052012-01-03 07:00:00dtype:datetime64[ns]In [14]:s.dt.dateOut[14]:02012-01-0112012-01-0122012-01-0132012-01-0242012-01-0252012-01-03dtype:objectIn [15]:s.dt.date.value_counts()Out[15]:2012-01-01 32012-01-02 22012-01-03 1dtype:int64
Solution 2:
Solution 3:
you can try this:
df.groupby(level=0).count()
this requires your date to be index.
Post a Comment for "Pandas: Counting Frequency Of Datetime Objects In A Column"