How to sum all the values under the same cateogory

I have a csv file where I would like to sum up the values under the same category so that when I hover over I can see the lump sum instead of each individual amount as per my csv file.

import plotly.express as px
import pandas as pd
import plotly.graph_objs as go
 
df = pd.read_csv('Revenue.csv')
df.head(20)
 
fig = px.bar(df,x='financial_year', y='amount', color='class',height=400, width=700, title=" Revenue")
fig.show()

Hi @unleashed ,

Welcome to forum.

You can manually sum the amount column based on category using groupby and create new column to store lump sum values.

For hover text, you need to modify hovertemplate property to add lump sum on all traces.

The code below is the solution that I offer:


import plotly.express as px
import pandas as pd

df = pd.read_csv('Revenue.csv')
fig = px.bar(df,x='financial_year', y='amount', color='class',height=400, width=700, title=" Revenue")

# create a new column as `lump_sump`
df['lump_sum'] = df['amount']

# group dataframe by column `class` and `financial_year`.
# and select only column `amount`
df_groupby= df.groupby(["class","financial_year"])["amount"]

# loop the group
for name,group in df_groupby:
	# `name` is combination group of columns (`class` and `financial_year`)
	class_cat, financial_year_cat =  name
	# get sum of every group and update into column `lump_sum`
	df.loc[group.index.tolist(),"lump_sum"] = group.sum()
	#  update info into `hovertemplate` on every trace by using `customdata` property
	fig.update_traces(customdata=df[(df['class']==class_cat)]['lump_sum'],hovertemplate='class: '+class_cat+' <br>financial_year: %{x} <br>amount: %{y} <br>Lump Sum: %{customdata:0.2f}',selector={'name':class_cat})

fig.show()

Hope this help.

Hi, thank you very much for your help.
I have tried out the coding. The bar chart in green are able to show the lump but not for the other 2 categories. How should I amend the code?

Hi @unleashed ,

Can you capture the image when hover the bar that has not lump?
or
maybe there is NaN values on data with non green bar
or
try to check the loop the group code block. I have updated the code.

Hope it works.