I have a pandas dataframe as below:
import pandas as pd
df = pd.DataFrame({'group':['A','A','A', 'B', 'B'],'date1':['12/1/2019','12/1/2019','12/1/2019', '12/1/2022', '12/1/2021'], 'nb_months':[11,11,12, 23, 15], 'col1':[1,1,2, 3, 5]})
df['date1'] = pd.to_datetime(df['date1'], format='%m/%d/%Y', errors='coerce').dropna()
df
group date1 nb_months col1
0 A 2019-12-01 11 1
1 A 2019-12-01 11 1
2 A 2019-12-01 12 2
3 B 2022-12-01 23 3
4 B 2021-12-01 15 5
I want to perform below operation,
IF first observation of group, then create new row/record where |date2| = |date1| - 1 Month and other values are left as missing ELSE |date2| = |date1|
My expected output
group date1 nb_months col1 date2
0 A 2019-11-01
1 A 2019-12-01 11 1 2019-12-01
2 A 2019-12-01 11 1 2019-12-01
3 A 2019-12-01 12 2 2019-12-01
4 B 2022-11-01
5 B 2022-12-01 23 3 2022-12-01
6 B 2021-12-01 15 5 2021-12-01