Khaine775
Khaine775

Reputation: 2765

Extracting year from date column using Panda

So I have a CSV containing a bunch of columns, Date being one of them, which I'm mostly interested in right now. I've managed to extract the column from the file, but I'm not interested in the entire date, only the 'year' part of it.

with open("/Users/thomasjn/Documents/sfpd_incidents.csv", 'r') as f:
    data = pd.read_csv(f)

    dates = data['Date'].str.split('/')

This is what I've done so far, which gives me a result like this:

0          [01, 19, 2015 12:00:00 AM]
1          [02, 01, 2015 12:00:00 AM]
2          [02, 01, 2015 12:00:00 AM]
3          [02, 01, 2015 12:00:00 AM]
4          [01, 27, 2015 12:00:00 AM]
5          [02, 01, 2015 12:00:00 AM]

But how do I remove everything from the date except the year?

Upvotes: 0

Views: 331

Answers (1)

Sebastian Wozny
Sebastian Wozny

Reputation: 17506

You can use Series.apply to transform your Series:

dates=dates.apply(lambda x: x[2])

This will give you only the third element of every list, which is the year you wanted.

Upvotes: 1

Related Questions