pandas 名称“pd”未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53116468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:07:44  来源:igfitidea点击:

name 'pd' is not defined

pythonpandas

提问by Bilal Khan

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Data.csv')
print(dataset)

Error:

错误:

dataset = pd.read_csv('Data.csv')
Traceback (most recent call last):

  File "<ipython-input-6-bd7168d85704>", line 1, in <module>
    dataset = pd.read_csv('Data.csv')

NameError: name 'pd' is not defined

回答by roganjosh

From your comments, you're using Spyder. The traceback confirms to me that you're running dataset = pd.read_csv('Data.csv')in the IPython interactive console.

根据您的评论,您正在使用 Spyder。回溯向我确认您正在dataset = pd.read_csv('Data.csv')IPython 交互式控制台中运行。

Spyder has configurable namespace sharing between scripts and the console. Running:

Spyder 在脚本和控制台之间具有可配置的命名空间共享。跑步:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

in a script makes all 3 modules accessible both to code within the script and within the interactive console. You could also run import pandas as pdin the console and then use pdthere indefinitely. This import mayor may notthen be available in your scripts depending on what settings you have used.

在一个脚本中,所有 3 个模块都可以在脚本中和交互式控制台中访问代码。您也可以import pandas as pd在控制台中运行,然后pd无限期地在那里使用。根据您使用的设置,此导入可能会可能不会在您的脚本中可用。

Your issue is that you either:

你的问题是你要么:

  1. haven't run import pandas as pdanywhere
  2. You've restarted the kernel at some point and lost your imports
  3. You've configured Spyder to wipe the namespace of your scripts each time they run, and haven't accounted for this.
  1. 没有跑到import pandas as pd任何地方
  2. 您在某个时候重新启动了内核并丢失了导入
  3. 您已将 Spyder 配置为每次运行时擦除脚本的命名空间,并且没有考虑到这一点。

Regardless of what settings you have with namespace sharing, alwaysimport modules into your scripts, don't rely on unusual quirks of Spyder because your code won't work elsewhere. What you do in the console is inconsequential.

无论您对命名空间共享进行了何种设置,始终将模块导入到您的脚本中,不要依赖 Spyder 的异常怪癖,因为您的代码在其他地方无法运行。您在控制台中执行的操作无关紧要。