我有一段时间前写的脚本有问题,几个月前,它工作正常,没有问题。但是,此后操作系统已更新。该脚本可以正常工作,直到尝试使用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)
...
我使用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
我的感觉熊猫有些问题,也许吗?我使用conda卸载了它,然后重新安装了它。尝试使用pip进行卸载,但从未使用pip进行安装,因此找不到它。我很茫然。
正如@EdChum在他们的评论中所说,问题是“在创建之前先引用索引”。具体来说,当您拥有时,您index=df.index
指的是的index
属性df
,但尚未创建df
,因此该属性不存在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句