Pandas未导入?’NameError: global name ‘pandas’ is not defined’

我遇到了一些错误,但我认为这是因为pandas没有导入,因为它显示为灰色。如果这是问题所在,我该如何解决这个问题?

C:\Anaconda\python.exe C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py Traceback (most recent call last): File “C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py”, line 38, in Key_Stats() File “C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py”, line 12, in Key_Stats df = pandas.DataFrame(columns = [‘Date’,’Unix’,’Ticker’,’DE Ratio’]) NameError: global name ‘pandas’ is not defined

Process finished with exit code 1

import pandas as pdimport osimport timefrom datetime import datetime#location of the data filespath = 'C:\Users\nickd\Documents\SKLEARN-STOCKS'#what specific field do you want to grab and in all files in that directorydef Key_Stats(gather="Total Debt/Equity (mrq) "):    statspath = path+'/_KeyStats'    stock_list = [x[0] for x in os.walk(statspath)]    df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])    for each_dir in stock_list[1:5]:        each_file = os.listdir(each_dir)        ticker = each_dir.split("//")[1]        if len(each_file) >0:            for file in each_file:                date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')                #unix_time = time.mktime(date_stamp.timetuple())                print(date_stamp,unix_time)                full_file_path = each_file+'/'+file                #print(full_file_path)                source = open(full_file_path, 'r').read()                #print(source)                try:                    value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])                    df = df.append({'Date':date_stamp,'Unix':unix_time,'Ticker':ticker,'DE Ratio':value,}, ignore_index = True)                except Exception as e:                    pass                #print(ticker+":",value)    save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv')    print(save)    df.to_csv(save)                #time.sleep(15)Key_Stats()

回答:

你已经导入为

import pandas as pd

但调用的是

#pandasdf = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

你可以选择以下两种方法之一进行修改:

  • import pandas as pd 改为 import pandas,或者
  • df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio']) 改为 df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])

编辑:

错误是由于缺少 )

save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv'))

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注