1
votes

I am trying to generate a plotly bar chart out of a grouped df. I have the data ordered by the groupby python statement. The data is arranged appropriately but I can't generate the plotly bar chart.

python is giving a value error ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['amount'] but received: Issued_Date

Additionally I would like the bar chart to be stacked. For example , row 0 and row 5 are the same date so i would like to have stacked bars

df_A = df_pre.groupby(['Transaction','Type'])["amount"].resample('M').sum().to_frame('amount')

fig = px.bar(df_A, x='Issued_Date', y='amount', color='Type',
             title='Timeseries amount',
             barmode='group',
             height=600
            )

fig.show()

df_A.to_dict('split')
{'index': [('No', 'B', Timestamp('2019-03-31 00:00:00')),
  ('No', 'E', Timestamp('2018-10-31 00:00:00')),
  ('No', 'H', Timestamp('2019-07-31 00:00:00')),
  ('So', 'B', Timestamp('2018-12-31 00:00:00')),
  ('So', 'E', Timestamp('2018-12-31 00:00:00')),
  ('So', 'H', Timestamp('2019-03-31 00:00:00')),
  ('So', 'H', Timestamp('2019-05-31 00:00:00')),
  ('So', 'H', Timestamp('2019-06-30 00:00:00')),
  ('So', 'H', Timestamp('2019-07-31 00:00:00'))],
 'columns': ['amount'],
 'data': [[39.21],
  [64.49],
  [572.78],
  [13.46],
  [44.54],
  [13.96],
  [0.0],
  [46.76],
  [13.28]]}
1
Is that a screenshot of dfA? Or something else?vestland
Yes. I didn't know how to submit a table , so I exported to csv and then copied the screenshot. This csv export was solely for the screenshot. hope it helpsPaul
Please share your data like thisvestland
{'Personal': {('No', 'E', Timestamp('2018-10-31 00:00:00')): 64.49, ('No', 'H', Timestamp('2019-07-31 00:00:00')): 572.78, ('So', 'H', Timestamp('2018-12-31 00:00:00')): 58.0, ('So', 'H', Timestamp('2019-01-31 00:00:00')): 0.0, ('So', 'H', Timestamp('2019-02-28 00:00:00')): 0.0, ('So', 'H', Timestamp('2019-03-31 00:00:00')): 39.21, ('So', 'E', Timestamp('2019-07-31 00:00:00')): 13.28}}Paul
I edited the post. Thanks again.Paul

1 Answers

0
votes

I referred to the official reference and saw your question applied to it.

import pandas as pd
import numpy as np
import io

data = '''
Transaction Type Issued_date Value
Div H 2020-2-9 84.5
Div H 2020-5-10 84.49
Div H 2020-6-7 78.13
Div I 2020-4-5 124.04
Div A 2020-2-9 40.65
Div A 2020-3-8 38.51
Div A 2020-11-12 39.99
Shell B 2020-9-16 -463.29
'''

df = pd.read_csv(io.StringIO(data), sep='\s+')

import plotly.express as px

fig = px.bar(df, x="Issued_date", y="Value", color="Type", title="Transaction", barmode='group', height=600)
fig.show()

enter image description here