Create a new Pandas column with only the year of the other column
I have the following df where 'per_end_date' is a Datetime object.
zacks_mktv_df[:4]
ticker | per_end_date | mkt_val
--------------------------------
A | 2016-12-31 | 14648.84
A | 2015-12-31 | 13704.02
A | 2014-12-31 | 13751.83
I want to grab the year from each row in the "per_end_date" column and make another row out of it, so for my table above it will look like.
ticker | per_end_date | mkt_val |
--------------------------------------------
A | 2016-12-31 | 14648.84 | 2016
A | 2015-12-31 | 13704.02 | 2015
A | 2014-12-31 | 13751.83 | 2014
I've tried this.
zacks_mktv_df['per_fisc_year'] = zacks_mktv_df[zacks_mktv_df.per_end_date.dt.year]
Getting the following error:
IndexError: indices are out-of-bounds
+3
source to share