how to use dataframe in dash? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how to use dataframe in dash? (/thread-11039.html) |
how to use dataframe in dash? - zhujp98 - Jun-19-2018 I am very new to python, need to develop some dash, I have a padas dataframe. df: x y1 y2 1 4 2 2 1 4 3 5 5 My question is how to use df to replace the hard coded 'data' part in following codes? dcc.Graph( id='example-graph', figure={ 'data': [ {'x': [1, 2, 3], 'y': [4, 1, 5], 'type': 'line', 'name': 'SF'}, {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'line', 'name': u'Montréal'}, ], 'layout': { 'title': ' Campaign Contacts ' } } )Thanks, Jeff RE: how to use dataframe in dash? - scidam - Jun-20-2018 data = pd.DataFrame({'x': [1,2,3], 'y1': [4,1,5], 'y2': [2, 4, 5]}) xvals = data.x.values.tolist() names = ['SF', 'Montreal'] data_pars = [{'x': xvals, 'y': data[colname].values.tolist(), 'name': name, 'type': 'line'} for colname, name in zip(data.iloc[:, 1:].columns, names)] dcc.Graph( id='example-graph', figure={ 'data': data_pars, 'layout': { 'title': ' Campaign Contacts ' } } ) |