2
$\begingroup$

I'm working on a column that I converted from a object to a datetime datatype in Pandas. I'm trying to get a count of how many of observations are there for each year.

This is the column: df_raw['filed_date'] and the output is:

198894   2017-12-05
198895   2017-12-05
198896   2017-12-05
198897   2017-12-06
198898   2017-12-06
198899   2017-12-07
Name: filed_date, Length: 198900, dtype: datetime64[ns]

I couldn't find a method to just access the date and then daisy chain it to the value_counts() method. Any advice?

$\endgroup$
1
$\begingroup$
from collecitons import Counter
Counter([date.year for date in df_raw['filed_date'].values])
| improve this answer | |
$\endgroup$
1
$\begingroup$

If you Have alreday converted it to datetime object, and want to extract other time related info, it can be done in this way,

pd.to_datetime(df_raw['time']).dt.year.value_counts()
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.