Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I solve list indices must be integers or slices not str error on Python?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading