Filipe Tabosa
Filipe Tabosa

Reputation: 303

How to replace a column in dataframe for the result of a function

currently I have a dataframe with a column named age, which has the age of the person in days. I would like to convert this value to year, how could I achieve that? at this moment, if one runs this command

df['age']

the result would be something like

0        18393
1        20228
2        18857
3        17623
4        17474
5        21914
6        22113
7        22584
8        17668
9        19834
10       22530
11       18815
12       14791
13       19809

I would like to change the value from each row to the current value/ 365 (which would convert days to year)

Upvotes: 0

Views: 30

Answers (1)

Corralien
Corralien

Reputation: 120399

As suggested:

>>> df['age'] / 365
         age
0  50.391781
1  55.419178
2  51.663014
3  48.282192
4  47.873973

Or if you need a real year:

>>> df['age'] // 365
   age
0   50
1   55
2   51
3   48
4   47

Upvotes: 1

Related Questions