Python increment rows starting from a month on month

81 Views Asked by At

How to obtain the below result

enter image description here

Current Month is the column which is to be calculated. We need to get the increment every month starting from Jan-18 for every account id.

Every Account First row/ Record will start from JAN-18, and Second Row will be Feb-18 an so on. We need to increment from Jan-18 till last observation is there for that account id

Above shown is for a sample account and the same has to be applied for multiple account id.

1

There are 1 best solutions below

1
On

You could achieve what you are looking for as follows:

import pandas as pd
from datetime import date

acct_id = "123456789"
loan_start_date = date(2018, 1, 31)
current_date = date.today()
dates = pd.date_range(loan_start_date,current_date, freq='M').strftime("%b-%y")

df_columns = [acct_id, loan_start_date, dates]
df = pd.DataFrame()
df["current_month"] = dates
df["acct_id"] = acct_id
df["loan_start_date"] = loan_start_date
df = df[["acct_id", "loan_start_date", "current_month"]]

print(df.head())