Mafeni Alpha
Mafeni Alpha

Reputation: 308

Remove Year From Pandas DataFrame

I have a Dataframe, df, with the following column:

df['DateofBirth'] =
    1   5/12/2005
    2   20/12/2009
    3   20/11/1991
    4   2/12/1997

The elements of the column are date/month/year. I want to make a new DataFrame with years only from this column. I have tried the solution here but its not working

Upvotes: 0

Views: 337

Answers (1)

crayxt
crayxt

Reputation: 2405

We can use str accessor together with split, if column is of string type and its format is constant.

>>> df
  DateofBirth
0   5/12/2005
1  20/12/2009
2  20/11/1991
3   2/12/1997
>>> df["DateofBirth"].str.split("/").str[2]
0    2005
1    2009
2    1991
3    1997
Name: DateofBirth, dtype: object

Upvotes: 2

Related Questions