0

I am looking to convert a normal dataframe with the columns ['Task','Start','Finish','Resource'] into the following format without doing it manually. Please note the = instead of : inbetween column and value... How would I convert this?

df = [dict(Task=“SH 5”, Start=‘2017-01-01’, Finish=‘2017-02-02’, Resource=‘Complete’),
dict(Task=“SH 5”, Start=‘2017-02-15’, Finish=‘2017-03-15’, Resource=‘Incomplete’),
dict(Task=“SH 5”, Start=‘2017-03-15’, Finish=‘2017-04-15’, Resource=‘Not Started’),
dict(Task=“SH 5”, Start=‘2018-03-15’, Finish=‘2018-04-15’, Resource=‘Complete’),
dict(Task=“Job-2”, Start=‘2017-01-17’, Finish=‘2017-02-17’, Resource=‘Not Started’),
dict(Task=“Job-2”, Start=‘2017-01-17’, Finish=‘2017-02-17’, Resource=‘Complete’),
dict(Task=“Job-3”, Start=‘2017-03-10’, Finish=‘2017-03-20’, Resource=‘Not Started’),
dict(Task=“Job-3”, Start=‘2017-04-01’, Finish=‘2017-04-20’, Resource=‘Not Started’),
dict(Task=“Job-3”, Start=‘2017-05-18’, Finish=‘2017-06-18’, Resource=‘Not Started’),
dict(Task=“Job-4”, Start=‘2017-01-14’, Finish=‘2017-03-14’, Resource=‘Complete’)]

Error:

ValueError: 
    Invalid value of type 'builtins.str' received for the 'annotations' property of layout
        Received value: "[{'x': Timestamp('2020-06-01 00:00:00'), 'y': 2, 'text': 'test'}, {'x': Timestamp('2020-07-01 00:00:00'), 'y': 5, 'text': 'test3'}]"

    The 'annotations' property is a tuple of instances of
    Annotation that may be specified as:
      - A list or tuple of instances of plotly.graph_objs.layout.Annotation
      - A list or tuple of dicts of string/value properties that
        will be passed to the Annotation constructor

Code:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.figure_factory as ff
import plotly.graph_objs as go

# figure
fig = ff.create_gantt(df)
ann = ann.to_dict('records')
ann = str(ann)

# plot figure
fig['layout']['annotations'] = ann
fig.show()
RCarmody
  • 712
  • 1
  • 12
  • 29
  • Can I ask what difference it makes which syntax is used to create the dict? Ultimately, it's going to be `:` as the printed key => value pair "delimiter" because that's how dicts are displayed. It's basically just a formatting/syntax artifact and the underlying data is the same. – ggorlen May 29 '20 at 16:04
  • Does this answer your question? [Pandas DataFrame to List of Dictionaries](https://stackoverflow.com/questions/29815129/pandas-dataframe-to-list-of-dictionaries) – ggorlen May 29 '20 at 16:04
  • Maybe you want strings instead of dicts? Or maybe you want to change the representation of a dict? – Riccardo Bucco May 29 '20 at 16:04
  • I added the code Im using and error that I'm receving... It works when I use a sample in the format above, but doesn't work when I create it myself with .to_dict('records') – RCarmody May 29 '20 at 16:14

0 Answers0