Hi,
I’m trying to create a pivot table, with
rows grouped by continent and country,
columns groupe by year
i would like to show sum of population and average LifeExp at the same time.
Is that posible?
My data is in this format:
Continent | Country | Pop | LifeExp | Year | |
---|---|---|---|---|---|
Africa | Angola | 100 | 77 | 2000 | |
Africa | Angola | 100 | 77 | 2020 | |
Europe | Denmark | 300 | 99 | 2000 | |
Europe | Denmark | 300 | 99 | 2020 | |
Europe | France | 400 | 76 | 2000 | |
Europe | France | 400 | 76 | 2020 | |
Africa | Niger | 200 | 88 | 2000 | |
Africa | Niger | 200 | 88 | 2020 |
I tried with this code, but I can’t add more than one value
from dash import Dash, html
import dash_pivottable
import pandas as pd
Load dataset
df = pd.read_csv(‘data.csv’)
app = Dash(name)
Define layout
app.layout = html.Div([
html.H1(children=‘Dashboard Pivot Table’),
dash_pivottable.PivotTable(
data=df.to_dict('records'), # Provide the data in dictionary format
rows=['continent'], # Group by continent
cols=['Year'], # No columns needed
vals=['pop', 'lifeExp'], # Values: population and life expectancy
aggregatorName='Sum', # Aggregator for both (we'll adjust this in the interface)
rendererName='Table' # Renderer to display the pivot table
)
])
Run the app
if name == ‘main’:
app.run_server(debug=True)