conversion of dataframe obtained from aggregate function

Viewed 20

Existing Dataframe :

                PushedDate                ID_number
                   amin         amax    
index           
7874            2022-02-18  2022-02-22       7874
4979            2021-10-06  2021-10-11       4979

I am looking for the dataframe , where i call use amin and amax as seperate columns :

               PushedDate_min   PushedDate_max   ID_number  
index           
7874            2022-02-18        2022-02-22         7874
4979            2021-10-06        2021-10-11         4979

Existing dataframe was obtained using below code :

aa = df.groupby(['ID_number']).agg({'PushedDate': [np.min, np.max]})
aa[aa.index.name] = aa.index 
aa.index.names = ['index']  # renaming the index
1 Answers

Instead, you can do:

aa = df.groupby(['ID_number']).agg({'PushedDate': [np.min, np.max]})
aa.columns = [f'{x}_{y}' for x,y in aa.columns]
aa = aa.reset_index()
Related