【问题标题】:Python in Jupyter Notebooks: 'Table' is not defined [closed]Jupyter Notebooks中的Python:未定义“表”[关闭]
【发布时间】:2020-02-07 00:10:11
【问题描述】:

我在 Jupyter Notebooks 中编写了以下代码用于练习:

method=['Take the Bus to Work', 'Drive to Work']
number_coworkers= [12, 15]
coffee=Table().with_columns('Method', method, 'Number of Coworkers', number_coworkers)
total_cups_day = coffee.column(0) * coffee.select("Average Cups of Coffee per Day")
number_cups_day_difference = total_cups_day.item(2) - total_cups_day.item(1)
number_cups_week_difference = number_cups_difference * 7
yearly_cups = number_cups_week_difference * 52

我收到以下错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-aad814252e94> in <module>
      6 method=['Take the Bus to Work', 'Drive to Work']
      7 number_coworkers= [12, 15]
----> 8 coffee=Table().with_columns('Method', method, 'Number of Coworkers', number_coworkers)
      9 total_cups_day = coffee.column(0) * coffee.select("Average Cups of Coffee per Day")
     10 number_cups_day_difference = total_cups_day.item(2) - total_cups_day.item(1)

NameError: name 'Table' is not defined

我在其他 Jupyter Notebooks 中看到了类似的方法,其中没有导入 Table,所以我不确定这个错误。

感谢您的帮助!

【问题讨论】:

  • 您在与astropy 合作吗? Table() might be a reference to this
  • 您必须导入Table。基于方法.with_columns(),看起来这是在datascience 包中。你可以使用from datascience.tables import Table
  • 你缺少以import ...开头的行
  • 您从该错误消息中了解/不了解什么?

标签: python jupyter-notebook


【解决方案1】:

您需要先安装包 datascience 才能使用 Table()。我使用 spyder 运行您的代码并执行了以下操作:

在 conda 终端中:

conda install datascience 

然后在代码中添加一个导入:

from datascience import * 

然后你就看不到错误了

【讨论】:

  • 请不要运行from datascience import *。它会使您的命名空间混乱。使用from datascience.tables import Tableimport datascience,然后使用datascience.Table
  • 请不要将此类信息分享为图片。请参阅:meta.stackoverflow.com/questions/303812/…。正如@jakub 所说,你不应该使用import *
猜你喜欢
  • 2018-08-21
  • 1970-01-01
  • 2018-05-31
  • 2021-12-02
  • 1970-01-01
  • 2023-03-15
  • 2019-04-11
  • 2018-10-31
  • 2021-09-10
相关资源
最近更新 更多