If I have a dataframe that looks like this:
Vintage CprTarget
0 2017 9.908900
1 2015 9.172600
2 2017 9.993500
3 2018 8.985600
4 2015 12.190200
... ... ...
20707 2020 5.559933
20708 2015 12.866399
20709 2019 17.982506
20710 2016 12.098302
20711 2015 11.390324
And Vintage
is type int64
how could I convert that column to instead be years before now? So instead it would look like:
Age CprTarget
0 5 9.908900
1 7 9.172600
2 5 9.993500
3 4 8.985600
4 7 12.190200
... ... ...
20707 2 5.559933
20708 7 12.866399
20709 3 17.982506
20710 6 12.098302
20711 7 11.390324
I know I can use today = date.today().year
to get the year now, but how could I grab the year from the Vintage
column to transform it to Age (and change it's name to Age
while we're at it).
CodePudding user response:
df['age'] = 2022 - df['Vintage']
df
or
df['age'] = pd.Timestamp.today().year - df['Vintage']
df
Vintage CprTarget age
0 2017 9.908900 5
1 2015 9.172600 7
2 2017 9.993500 5
3 2018 8.985600 4
4 2015 12.190200 7
20707 2020 5.559933 2
20708 2015 12.866399 7
20709 2019 17.982506 3
20710 2016 12.098302 6
20711 2015 11.390324 7
CodePudding user response:
Let us do
df['age'] = df.Vintage.rsub(pd.Timestamp.today().year)