Home > database >  pandas aggregate on Date column
pandas aggregate on Date column

Time:12-08

Below is sample pandas code and result.I am trying to convert Date list to date format excluding the timestamp i.e just the date eg:2022-06-28., but unable to get the result. Any help is much appreciated.

df2=df1.sort_values(['Date'],ascending=False).groupby(['Remarks']) 
   ['dr','cr','Date'].agg({'dr':['sum',list],'cr':['sum',list],'Date':list}).reset_index()


  Remarks     dr               cr                   Date
              sum        list        sum     list   list
0   peta  10000.00     [10000.0]     0.0      [nan] [2022-06-28 00:00:00]
1   axis    227222.00   [227222.0]   0.0      [nan] [2022-12-05 00:00:00]

Thanks in advance.

CodePudding user response:

first convert date list to date

df2['Date'] = pd.to_datetime(df2['Date'])
df2['Date'] = pd.to_datetime(df2['Date']).dt.date #if you want only date
df2=df1.sort_values(by='Date',ascending=False).groupby(['Remarks']) 

CodePudding user response:

You could format the date before running groupby command.

df1['Date_wo_timestamp'] = df1['Date'].str.split(' ').str[:1]
  • Related