How the new column df['month] to be able to become one of the column in the data set so it can be a filter criteria in my groupby?
Original data:
Date Sales
01/01/2022 1000
01/01/2022 100
08/01/2022 200
02/03/2022 2000
02/03/2022 200
02/03/2022 400
12/05/2022 6
20/05/2022 8
25/05/2022 200
01/06/2022 300
25/06/2022 400
25/06/2022 5
01/02/2022 1
01/02/2022 2
02/04/2022 2
02/04/2022 4
month_labels = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug',9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
df['month'] = pd.to_datetime(df['Settlement_Date'], format = '%d/%m/%Y', errors='coerce').dt.month
df.sort_values(by= ['month', 'Settlement_Date'], inplace = True)
Using the sort_values to rearrange the date.
Arranged data:
Date Sales
01/01/2022 1000
01/01/2022 100
08/01/2022 200
01/02/2022 1
01/02/2022 2
02/03/2022 2000
02/03/2022 200
02/03/2022 400
02/04/2022 2
02/04/2022 4
12/05/2022 6
20/05/2022 8
25/05/2022 200
01/06/2022 300
25/06/2022 400
25/06/2022 5
def update_graph(dd_selection,mth_selection):
if len (dd_selection) and len (mth_selection) ==0:
return dash.no_update
else:
dff1= df[df['Settlement_Date'] == dd_selection]
dff2= df[df['month'] == mth_selection]
bar_groupby1 = df[df['month'] < mth_selection ].groupby(['Date', 'month'])['Sales'].agg(['sum']).reset_index().rename(columns={'sum':'Total_Tx_Amount'})
fig = px.bar(bar_groupby1, x='month', y='Total_Tx_Amount', title='Bar_chart', color='month')
return fig
The df['month'] is not function in the bar_groupby1.
Error message:
TypeError: Invalid comparison between dtype=int64 and str