Python Pandas df 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34045291/
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
Python Pandas df is not defined
提问by Clemens
I have a problem with a script I wrote a while back, couple of months ago it worked fine without problem. However since then the OS has been updated. The script works fine until it tries to create a dataframe with pandas
我之前写的一个脚本有问题,几个月前它运行良好,没有问题。然而,从那时起,操作系统已经更新。该脚本工作正常,直到它尝试使用 Pandas 创建数据框
import os
import pandas as pd
import matplotlib.pyplot as plt
dir_input = '/home/xxx/xxx/xxx/Script/input/'
osdir = []
alldir = []
for all_files in os.listdir(dir_input):
alldir.append(all_files)
for file in os.listdir(dir_input): #Adds all the specified files to the list osdir
if file.endswith('.xlsx'):
osdir.append(file)
print("Found {0}".format(file))
for filename in osdir:
(fileroot, extension) = os.path.splitext(filename)
print 'Processing file...'
print fileroot
print ''
# pandas works with so called dataframes to import the data. Since I dont need all the columns we only use column d,f and j
df = pd.read_excel(dir_input+filename,parse_cols="D,F,J", index=df.index)
...
The error I get using spyder
我在使用 spyder 时遇到的错误
Traceback (most recent call last):
File "<ipython-input-5-2cf9c86bcb8c>", line 1, in <module>
runfile('/home/xxx/python_scripts/xpos-frame-mean_batch_v1.1.py', wdir='/home/cdoering/python_scripts')
File "/home/xxx/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "/home/xxx/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/home/xxx/python_scripts/script.py", line 54, in <module>
df = pd.read_excel(dir_input+filename,parse_cols="D,F,J", index=df.index)
NameError: name 'df' is not defined
My feeling is there is something wrong with pandas, maybe? I uninstalled it using conda and reinstalled it. Tried uninstalling with pip, but never used pip to install it so it couldn't find it. I am at a loss.
我的感觉是大Pandas有问题,也许?我使用 conda 卸载它并重新安装它。尝试用pip卸载,但从未使用pip安装它所以找不到它。我很茫然。
采纳答案by dmh
As @EdChum said in their comment, the problem is 'referencing the index prior to creation'. Specifically, when you have index=df.index
you are referring to the index
attribute of the df
, but you haven't created the df
yet, so that attribute doesn't exist.
正如@EdChum 在他们的评论中所说,问题是“在创建之前引用索引”。具体来说,当您拥有index=df.index
的index
属性时df
,但您尚未创建df
,因此该属性不存在。