Hi,
i have a set of data from excel .csv.
I need to make a .groupby().sum() within the set of data in python and display the calculated data into my plotly table based on the date selected.
Set of data:
Expected result in plotly table:
How can I made it?
Easiest is to use Pandas:
import pandas as pd
df = pd.DataFrame(
{
"Date": ["01/01/2022", "01/01/2022", "01/01/2022", "01/01/2022", "01/01/2022", "01/01/2022", "01/01/2022", "02/01/2022"],
"Minor": ["Cutlery", "Tops", "Shoes", "Turf", "Hoses", "Rakes", "Cooking", "Cutlery"],
"Major": ["Kitchen", "Clothes", "Clothes", "Garden", "Garden", "Garden", "Kitchen", "Kitchen"],
"MTD": [1000, 100, 200, 1, 2, 3, 4, 2000],
}
)
print(df)
print(df.groupby(["Date", "Major"]).sum())
Result:
MTD
Date Major
01/01/2022 Clothes 300
Garden 6
Kitchen 1004
02/01/2022 Kitchen 2000
Since you are getting the data from a CSV, you can use pandas.read_csv:
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
dff = df.groupby(['Date','Major'])['MTD'].agg(['sum']).reset_index().rename(columns={'sum':'Total_Amount'})
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
html.H3('Table'),
dt.DataTable(id='table', columns=[
{"name":i, "id":i}for i in (dff.columns)],
])
])
])
@app.callback(
Output('table','data'),
Input('date_dd', 'value')
)
def update_data (selection):
if selection:
dff1 = dff[dff.Date == selection]
data1 = dff1.to_dict('rows')
return data1
This is what I trying,
The result I get:
If I only want the dashboard display the columns that I needed, such as Major and MTD.
This part I still not able to get it.