Matt
Matt

Reputation: 69

Pandas: changing years based on an int value

I'm trying to subtract years from one column based on a number in another column.

This is what i mean:

   base_date          amount_years
0  2006-09-01         2
1  2007-04-01         4

The result would be:

   base_date          amount_years
0  2008-09-01         2
1  20011-04-01        4

Is there a way to achieve this in python?

Upvotes: 1

Views: 70

Answers (1)

jezrael
jezrael

Reputation: 863056

Use DateOffset with apply and axis=1 for process per rows:

f = lambda x: x['base_date'] + pd.offsets.DateOffset(years=x['amount_years'])
df['base_date'] = df.apply(f, axis=1)
print (df)
   base_date  amount_years
0 2008-09-01             2
1 2011-04-01             4

Upvotes: 1

Related Questions