1

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]]}
Paul
  • 113
  • 5
  • 20
  • Is that a screenshot of `dfA`? Or something else? – vestland Aug 18 '20 at 12:24
  • 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 helps – Paul Aug 18 '20 at 19:43
  • Please share your data like [this](https://stackoverflow.com/questions/63163251/pandas-how-to-efficiently-build-and-share-a-sample-dataframe/63163254#63163254) – vestland Aug 18 '20 at 20:50
  • {'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 Aug 18 '20 at 21:48
  • Thank you. to_dict() was good except stackoverflow does not allow hardly characters in the dict. I had to delete many rows. – Paul Aug 18 '20 at 21:51
  • We normally only need a small sample of your data to reproduce your issue. So take a look at the linked post again, particularly the part covering `df.tail(10).to_dict()` – vestland Aug 18 '20 at 22:03
  • And please share the data in the question. Not as a comment. – vestland Aug 18 '20 at 22:32
  • 1
    I edited the post. Thanks again. – Paul Aug 18 '20 at 23:32
  • This does not make very much sense. Your `fig` is built on `df_A` which is built on `df_pre` which has ***not*** been defined. Adding `df_A.to_dict` *after* those steps is uselelss. In addttion, you've applied the function wrong. Again, please read [this](https://stackoverflow.com/questions/63163251/pandas-how-to-efficiently-build-and-share-a-sample-dataframe-using-df-to-dict) *thoroughly* so that you may enable yourself to share a pandas datasample. – vestland Aug 19 '20 at 07:54
  • And the data you need to share seems to be `df_pre` – vestland Aug 19 '20 at 07:55

1 Answers1

0

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

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Great. Although, I will be more clear I would rather generate the graph directly from the multiindex df and eliminate the exporting and importing. This exporting and importing will be an unnecessary step though. – Paul Aug 18 '20 at 19:50