I have code block like below. When I run the code I get "list indices must be integers or slices not str" error. How can I solve it ?
The code:
import nasdaqdatalink
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import scale
import matplotlib.dates
import math
import datetime
style.use('ggplot') #for graphics
api_key = ('xyz') #api key for Nasdaq data
nasdaqdatalink.apiconfig = api_key
df = nasdaqdatalink.get('BITFINEX/BTCUSD') #bitcoin data from Nasdaq
df.dropna(inplace = True) #to delete NAN variables of data
df['HL_PCT'] = (df['High'] - df['Low']) / df['Last'] * 100.0 #for produce parameter
df['ASKBID_PCT'] = (df['Ask'] - df['Bid']) / df['Last'] * 100.0 #for produce parameter
df = [['High', 'Low', 'HL_PCT', 'ASKBID_PCT','Volume']]
forecast_out = int(math.ceil(len(df) * 0.01)) #multiply the length of the rows by 1% and round the dataframe up
df['Label'] = df['Last'].shift(-forecast_out) #forecast_out value is moved up to assume today's value as the last days value
I get the error last row of code block like below.
The error:
df['Label'] = df['Last'].shift(-forecast_out) #forecast_out value is moved up to assume today's value as the last days value
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 1
----> 1 df['Label'] = df['Last'].shift(-forecast_out)
TypeError: list indices must be integers or slices, not str
>Solution :
You need to change line:
df = [['High', 'Low', 'HL_PCT', 'ASKBID_PCT','Volume']]
to
df = df[['High', 'Low', 'HL_PCT', 'ASKBID_PCT', 'Volume']]
This will redefine df to only include the columns specified, whereas your original code redefined the variable as a list of strings, causing the error you see to occur the next time you tried accessing a column from df.