简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM