I have a dataframe that looks like this where the 'dni' is a unique id for each row. I want to use plotly for a stacked barplot across the dates column for each individual dni.
| dni | date | col1 | col2 | col3 |
|---|---|---|---|---|
| unique ids | Datetime | 5 | 7 | 1 |
The code I'm currently using looks like this:
fig = go.Figure()
buttons = []
for i,dni in enumerate(sorted(df_merged.dni.unique())):
df = df_merged[df_merged['dni']==dni]
for column in df.columns[3:-1]:
fig.add_trace(go.Bar(
name = column,
x = pd.to_datetime(df.date.astype('str')),
y = df[column],
visible = (i==0)
))
args = [False] * df_merged.dni.nunique()
args[i] = True
button = dict(label = dni,
method = "update",
args=[{"visible": args}])
buttons.append(button)
fig.update_layout(
updatemenus=[
dict(
type="dropdown",
direction="down",
buttons = buttons)
],
barmode = "stack",)
fig.show()
This does give me a plot with filters but the data it shows is always incorrect. I'm struggling to understand where I'm going wrong. The only correct data it shows is for the first dni which also gets changed when I actually press the button.

Thanks!
EDIT: This is the actual data:
id date bills goalTrans incomes payments savings
0 12345678901 2020-12 1.0 2.0 1.0 0.0 0.0
1 23456789012 2021-02 6.0 0.0 2.0 0.0 0.0
2 34567890123 2020-12 4.0 0.0 2.0 0.0 0.0
3 45678901234 2020-12 9.0 1.0 1.0 0.0 0.0
4 56789012345 2021-01 3.0 0.0 2.0 1.0 0.0
{'bills': {0: 1.0, 1: 6.0, 2: 4.0, 3: 9.0, 4: 3.0},
'date': {0: '2020-12',
1: '2021-02',
2: '2020-12',
3: '2020-12',
4: '2021-01'},
'id': {0: '12345678901',
1: '23456789012',
2: '34567890123',
3: '45678901234',
4: '56789012345'},
'goalTrans': {0: 2.0, 1: 0.0, 2: 0.0, 3: 1.0, 4: 0.0},
'incomes': {0: 1.0, 1: 2.0, 2: 2.0, 3: 1.0, 4: 2.0},
'payments': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 1.0},
'savings': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}}

