gpt4 book ai didi

python - Pandas 不进口吗? 'NameError: global name ' pandas'未定义'

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:48 24 4
gpt4 key购买 nike

我在这里遇到了一些错误,但我认为这是因为 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 pd
import os
import time
from datetime import datetime

#location of the data files
path = 'C:\Users\nickd\Documents\SKLEARN-STOCKS'
#what specific field do you want to grab and in all files in that directory
def 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

和调用

#pandas
df = 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'))

关于python - Pandas 不进口吗? 'NameError: global name ' pandas'未定义',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com