Harsha Vardhan
Harsha Vardhan

Reputation: 139

Extract year portion from date-string column

I am new to Pandas

I am accessing the date column which is in the format of

Restaurent    ISSDTM
CREAMERY INC 4/5/2013 12:47
CREAMERY INC 4/5/2013 12:47
SANDRA       3/5/2009 11:23
SANDRA       8/26/2009 13:11

print(df['ISSDTTM'].dtype)--> Is an object

I want to do a count plot for this as per the year. I tried using the `df1=df['ISSDTTM'].apply(lambda x:x.split('/'))

to access the date but I am unable` to split the space in between. Also,

df1=df['ISSDTTM'].apply(lambda x:x.split(['/',' '])) 

didn't work.

I also tried to access the last 4 digits using the

df2=df['ISSDTTM'].apply(lambda x:x[-1:-4]) 

Any approach to split this type of date formats? Should I use the dt.strformat?

Upvotes: 1

Views: 1627

Answers (1)

cs95
cs95

Reputation: 402493

Yes, you were on the right track with dt. Coerce to datetime and use dt.year.

pd.to_datetime(df.ISSDTM, errors='coerce').dt.year

0    2013
1    2013
2    2009
3    2009
Name: ISSDTM, dtype: int64 

You can use DataFrame.plot.bar, or seaborn.countplot to generate a count-plot.

Upvotes: 2

Related Questions