Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 TypeError: list indices must be integers or slices, not str
#1
So I am new to python and I can't seem to find the error with this code, when I try to run I get the message "TypeError: list indices must be integers or slices, not str". My code is:

import pandas as pd
import quandl


df = quandl.get("WIKI/GOOGL", authtoken="X5A1-ewy2UCfkFVfGKuW")
df = [["Adj. Open","Adj. High","Adj. Low","Adj. Close","Adj. Volume",]]
df["HL_PCT"] = (df["Adj. High"] - df["Adj. Close"]) / df["Adj. Close"] * 100.0
df["PCT_change"] = (df["Adj. Close"] - df["Adj. Open"]) / df["Adj. Open"] * 100.0

df = df [["Adj. Close","HL_PCT","PCT_change","Adj. Volume"]]

print(df.head())
Any help would be appiciated Big Grin
Quote
#2
So I am having this problem with my code. I can't figure out why it is working. I am very new to python and would appreciate some help.
The code is:

import pandas as pd
import quandl


df = quandl.get("WIKI/GOOGL", authtoken="X5A1-ewy2UCfkFVfGKuW")
df = [["Adj. Open","Adj. High","Adj. Low","Adj. Close","Adj. Volume",]]
df["HL_PCT"] = (df["Adj. High"] - df["Adj. Close"]) / df["Adj. Close"] * 100.0
df["PCT_change"] = (df["Adj. Close"] - df["Adj. Open"]) / df["Adj. Open"] * 100.0

df = df [["Adj. Close","HL_PCT","PCT_change","Adj. Volume"]]

print(df.head())

And the error I am getting is:
Error:
RESTART: C:\Users\elias\Desktop\PythonStuff\PythonProjects\MachineLearning.py Traceback (most recent call last): File "C:\Users\elias\Desktop\PythonStuff\PythonProjects\MachineLearning.py", line 7, in <module> df["HL_PCT"] = (df["Adj. High"] - df["Adj. Close"]) / df["Adj. Close"] * 100.0 TypeError: list indices must be integers or slices, not str
Thx :)
Quote
#3
Hi,
I have checked it with some GOOGL WIKI DATA SET from quandl.Run the following code...

import pandas as pd
df = pd.read_csv("C:\\Users\\Manas\\Desktop\\Misc\\WIKI-PRICES.csv") #.....(of course you have to change the path of your data set)
print(df.head())
df = df[['adj_open','adj_high','adj_low','adj_close','adj_volume']] #.....(Replace with your own features...Adj. Open etc)
df['HL_PCT'] = (df['adj_high'] - df['adj_low'])/df['adj_close']*100
df['PCT_change'] = (df['adj_close'] - df['adj_open']) / df['adj_open'] * 100.0
df = df[['adj_close', 'HL_PCT', 'PCT_change', 'adj_volume']]
print(df.head())
buran wrote Mar-06-2018, 11:19 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#4
(Mar-05-2018, 05:25 PM)TheDovah7 Wrote: I can't figure out why it is working.
At line 6 you redefine df to be a list. After that, df["some string"] must fail.
buran likes this post
Quote
#5
Thx for the help. I noticed that I missed a df in one of the lines XD Thx for helping me find it tho, would have never found it myself.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  monthly composite of LST comes out incorrect, with error "mean of empty slices" ns423 1 839 Mar-18-2018, 06:40 PM
Last Post: ns423
  Making indices and rearrranging list reidybwoykeon 2 1,457 May-18-2017, 06:57 PM
Last Post: reidybwoykeon

Forum Jump:


Users browsing this thread: 1 Guest(s)