Danish M
Danish M

Reputation: 105

How to count how many times every year shows up in my dataset in python

wondering if anyone can help me.

I have a dataset with the column "created_at" which has rows like this

data = pd.read_csv("dataset.csv")
col = data["created_at"]
print(col.head())
print(col.tail())

0    2014-06-01 21:03:16
1    2014-06-01 09:06:48
2    2014-06-01 00:31:52
3    2014-06-04 10:04:47
4    2014-06-04 10:05:40
Name: created_at, dtype: object
380064    2019-05-31 23:49:39
380065    2019-05-31 23:52:34
380066    2019-05-31 23:27:28
380067    2019-05-31 14:01:31
380068    2019-05-31 12:30:33
Name: created_at, dtype: object

I'm trying to count how many times each year appears so how many times does the year 2014 appear and 2015 and so on.

I've tried counters and for loops but I just can't seem to get it to work. If anyone can help, would be greatly appreciated

Upvotes: 0

Views: 249

Answers (1)

Ankur Sinha
Ankur Sinha

Reputation: 6639

First convert your column into datetime type because I see that it is in object type:

data['created_at'] = pd.to_datetime(data['created_at'])

Now extract the year part using dt:

data['year'] = data['created_at'].dt.year

Finally, do the count using value_counts:

data.year.value_counts()

Sample Output:

data.year.value_counts()
Out[142]: 
2014    3
2015    2
Name: year, dtype: int64

Upvotes: 1

Related Questions