pandas 为什么我的熊猫数据框没有定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42320393/
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
Why is my pandas dataframe not defined
提问by Brian Pozza
I create a data frame as follows (I am using PyCharm and Python 3.6):
我创建了一个数据框如下(我使用的是 PyCharm 和 Python 3.6):
import pandas as pd
data1=pd.read_csv('LoanDataReport_jan_thru_may.txt', sep='|',
low_memory=False)
data2=pd.read_csv('LoanDataReport_jun_thru_sep.txt', sep='|',
low_memory=False)
data3=pd.read_csv('LoanDataReport_oct_thru_dec.txt', sep='|',
low_memory=False)
datafinal=pd.concat([data1,data2,data3])
print(datafinal)
This runs as expected and displays a portion of datafinal.
这按预期运行并显示 datafinal 的一部分。
THEN, I comment out the lines of code I just ran using # and add a new line of code to get the dtypes. It looks like this:
然后,我注释掉我刚刚使用 # 运行的代码行并添加新的代码行来获取 dtypes。它看起来像这样:
#import pandas as pd
#
#data1=pd.read_csv('LoanDataReport_jan_thru_may.txt', sep='|',
# low_memory=False)
#
#data2=pd.read_csv('LoanDataReport_jun_thru_sep.txt', sep='|',
# low_memory=False)
#
#data3=pd.read_csv('LoanDataReport_oct_thru_dec.txt', sep='|',
# low_memory=False)
#
#datafinal=pd.concat([data1,data2,data3])
#
#print(datafinal)
#
print(datafinal.dtypes)
I get the following error: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/myname/Desktop/Python3/Import Data.py" Traceback (most recent call last): File "/Users/myname/Desktop/Python3/Import Data.py", line 17, in print(datafinal.dtypes) NameError: name 'datafinal' is not defined
我收到以下错误:/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/myname/Desktop/Python3/Import Data.py" Traceback (last last call last): File "/ Users/myname/Desktop/Python3/Import Data.py", line 17, in print(datafinal.dtypes) NameError: name 'datafinal' is not defined
Process finished with exit code 1
进程以退出代码 1 结束
WHY DOES THIS OCCUR? WHY AM I NOT ABLE TO CONTINUE WORKING ON datafinal WITHOUT RUNNING THE ENTIRE CODE EVERYTIME? ALL HELP IS GREATLY APPRECIATED.
为什么会发生这种情况?为什么我不能在不用每次都运行整个代码的情况下继续处理 datafinal?非常感谢所有帮助。
回答by wombatonfire
I recommend to read about variables and scopein Python.
我建议阅读Python 中的变量和作用域。
You have a standalone script where you define a global variable datafinal
. That variable is visible only during a script lifetime and it's not preserved between your first and second attempts. When you run your script a second time, after you have commented all but the last statement, you are trying to access a varible that does not exist.
您有一个独立的脚本,您可以在其中定义一个全局变量datafinal
。该变量仅在脚本生命周期内可见,并且不会在您第一次和第二次尝试之间保留。当您第二次运行您的脚本时,在您对除最后一条语句之外的所有语句进行注释之后,您正试图访问一个不存在的变量。
If you would like to explore your data in a gradual manner, look at the Jupyter Notebook. One way to get it is to use AnacondaPython distribution.
如果您想逐步探索您的数据,请查看Jupyter Notebook。获得它的一种方法是使用AnacondaPython 发行版。