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
name 'pd' is not defined
提问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 pd
in the console and then use pd
there 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:
你的问题是你要么:
- haven't run
import pandas as pd
anywhere - You've restarted the kernel at some point and lost your imports
- You've configured Spyder to wipe the namespace of your scripts each time they run, and haven't accounted for this.
- 没有跑到
import pandas as pd
任何地方 - 您在某个时候重新启动了内核并丢失了导入
- 您已将 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 的异常怪癖,因为您的代码在其他地方无法运行。您在控制台中执行的操作无关紧要。