1

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

1 Answer 1

1

Use DataFrame.drop_duplicates for duplicated rows, add column with subtract 1 month, add to original by concat and last sorting with reindex for original order of columns:

df1 = (df.drop_duplicates('group')
         .assign(date2 = lambda x: x['date1'] - pd.offsets.DateOffset(months=1)))

df = (pd.concat([df1[['group', 'date2']],  
                 df.assign(date2 = lambda x: x['date1'])], sort=False)
       .sort_values('group')
       .reindex(columns=df.columns.tolist() + ['date2'])
       .reset_index(drop=True))
print (df)
  group      date1  nb_months  col1      date2
0     A        NaT        NaN   NaN 2019-11-01
1     A 2019-12-01       11.0   1.0 2019-12-01
2     A 2019-12-01       11.0   1.0 2019-12-01
3     A 2019-12-01       12.0   2.0 2019-12-01
4     B        NaT        NaN   NaN 2022-11-01
5     B 2022-12-01       23.0   3.0 2022-12-01
6     B 2021-12-01       15.0   5.0 2021-12-01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.