【问题标题】:How can we assign dictionary titles to items in a list?我们如何将字典标题分配给列表中的项目?
【发布时间】:2021-02-11 18:53:55
【问题描述】:

我有一个看起来像这样的数据框。

df = df_gantt.values.tolist()
df

我正在尝试用它创建一个甘特图。这是我的代码。

import plotly.figure_factory as ff 
df = df_gantt.values.tolist()
fig = ff.create_gantt(df) 
fig.show()

这是我得到的错误。

---------------------------------------------------------------------------
PlotlyError                               Traceback (most recent call last)
<ipython-input-52-fdb705605b1c> in <module>
      2 df = df_gantt.values.tolist()
      3 
----> 4 fig = ff.create_gantt(df)
      5 fig.show()

/usr/local/lib/python3.8/site-packages/plotly/figure_factory/_gantt.py in create_gantt(df, colors, index_col, show_colorbar, reverse_colors, title, bar_width, showgrid_x, showgrid_y, height, width, tasks, task_names, data, group_tasks, show_hover_fill)
    943     """
    944     # validate gantt input data
--> 945     chart = validate_gantt(df)
    946 
    947     if index_col:

/usr/local/lib/python3.8/site-packages/plotly/figure_factory/_gantt.py in validate_gantt(df)
     64         )
     65     if not isinstance(df[0], dict):
---> 66         raise exceptions.PlotlyError("Your list must only " "include dictionaries.")
     67     return df
     68 

PlotlyError: Your list must only include dictionaries.

我做错了什么?我正在尝试从这里学习示例。

import plotly.figure_factory as ff 
  
df = [dict(Task="A", Start='2020-01-01', Finish='2009-02-02'), 
      dict(Task="Job B", Start='2020-03-01', Finish='2020-11-11'), 
      dict(Task="Job C", Start='2020-08-06', Finish='2020-09-21')] 
  
fig = ff.create_gantt(df) 
fig.show()

https://www.geeksforgeeks.org/gantt-chart-in-plotly/

当我尝试运行它时。

df_gantt = df_final[['submarket','inservice_activation','real_estate_completed']]
import plotly.figure_factory as ff
df = df_gantt.to_dict('records')
fig = ff.create_gantt(df) 
fig.show()

我收到此错误。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-62-5cb656f7907a> in <module>
      4 import plotly.figure_factory as ff
      5 df = df_gantt.to_dict('records')
----> 6 fig = ff.create_gantt(df)
      7 fig.show()

/usr/local/lib/python3.8/site-packages/plotly/figure_factory/_gantt.py in create_gantt(df, colors, index_col, show_colorbar, reverse_colors, title, bar_width, showgrid_x, showgrid_y, height, width, tasks, task_names, data, group_tasks, show_hover_fill)
    976                 "assigning colors to particular values in a dictioanry."
    977             )
--> 978         fig = gantt(
    979             chart,
    980             colors,

/usr/local/lib/python3.8/site-packages/plotly/figure_factory/_gantt.py in gantt(chart, colors, title, bar_width, showgrid_x, showgrid_y, height, width, tasks, task_names, data, group_tasks, show_hover_fill, show_colorbar)
     96     for index in range(len(chart)):
     97         task = dict(
---> 98             x0=chart[index]["Start"],
     99             x1=chart[index]["Finish"],
    100             name=chart[index]["Task"],

KeyError: 'Start'

【问题讨论】:

  • df = df_gantt.values.tolist() 看起来像什么?它是示例中的字典列表吗?
  • 不,WBM,它看起来像我在图片中显示的,在我原来的帖子的更新中。

标签: python python-3.x dictionary plotly gantt-chart


【解决方案1】:

第一个问题与您的命名约定有关,您需要重命名列以匹配示例:

df_gant.rename(columns = {"old name1": "Start"}, inplace=True)
df_gant.rename(columns = {"old name2": "Finish"}, inplace=True)
df_gant.rename(columns = {"old name3": "Task"}, inplace=True)

接下来,您需要将数据框更改为字典列表,而不是将数据框转换为值列表。试试这个:

import plotly.figure_factory as ff
df = df_gant.to_dict('records')
fig = ff.create_gantt(df) 
fig.show()

查看有关将数据帧转换为字典列表的更多详细信息here

【讨论】:

  • 谢谢,但是当我尝试运行它时,我得到了一堆错误。我刚刚更新了我原来的帖子,出现了我现在看到的错误。
  • 你检查过字典列表的样子吗?它与示例相符吗?甘特图需要一个“开始”和一个“完成”日期时间变量。所以你需要重命名你的变量
  • 这是一个特定的 KeyError。查看我的答案的更新。
  • 我在逗号前添加了结尾的 '}',效果很好。非常感谢!!!!
猜你喜欢
  • 2014-10-16
  • 1970-01-01
  • 2021-04-02
  • 2020-10-08
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多