What does the error "list indices must be integers or slices, not str" mean and how can I fix it?

Jaime Miranda

I'm reading some data from Excel using Pandas in Python, this is how I read the data:

data_frame = pd.read_excel(file_location, usecols=catcode_column)
data_frame2 = pd.read_excel(file_location, usecols=smpl_column)

where the file_location is a string of the path and the usecols is simply the column that I want to read from the Excel file. Then I have 2 for loops, the first is is to just use the first 5 characters on the given column and the second uses the entire string:

    for i in data_frame:
        data_frame["catcode_column"] = data_frame[catcode_column_name].apply(lambda x: str(x)[:5])
    for j in data_frame2:
        data_frame2["smpl_column"] = data_frame2[smpl_column_name].apply(lambda y: y[:])

Now, I convert those data frames into lists using the following:

catcode_array = data_frame["catcode_column"].values.tolist()
smpl_array = data_frame2["smpl_column"].values.tolist()

and the the output when printing the catcode_array and smpl_array give me is the following:

Catcode Array: ['34123', '42253', '63334']

SMPL Array: ['Apartment-Midrise', 'Apartment-Midrise', 'Apartment-Midrise']

Now, I have a new array initialized to None with a size of 10000 and a counter initialized to 0. I iterate through the smpl_array, which is printed above, and if the index i of smpl_array is equal to the building_type_array index 0, then I want to store the value of the catcode_array at index i in the apartment_midrise at counter1 which at the beginning will be 0 so at the first index of that array and then increment the counter. It is worth nothing that building_type_array is an array of strings.

apartment_midrise = [None] * 10000

counter1 = 0

for i in smpl_array:
    if smpl_array[i] == building_type_array[0]:
        apartment_midrise[counter1] = catcode_array[i]
        counter1 += 1

But, I'm getting the following error in the if statement above:

if smpl_array[i] == building_type_array[0]:

TypeError: list indices must be integers or slices, not str

I've been trying to fix this without any luck. I would appreciate any help. Thanks!

Gabip

In order to loop over a list and get the item and it's index in each iteration, you can use enumerate:

for i, smpl in enumerate(smpl_array):
    if smpl == building_type_array[0]:
        apartment_midrise[counter1] = catcode_array[i]
        counter1 += 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how I can fix this error? TypeError: list indices must be integers or slices, not str

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

how to solve type error: "list indices must be integers or slices, not str"

Can you please help me fix the (TypeError: list indices must be integers or slices, not str) in the code

I get the error "TypeError: list indices must be integers or slices, not str" when trying to request something that is in square brackets

when i try to print data, error is appeared. TypeError: list indices must be integers or slices, not str

I can't iterate through my list, it just says "list indices must be integers or slices, not str"

How to Filter pandas dataframe [Error : list indices must be integers or slices, not str]

error list indices must be integers or slices, not str

"list indices must be integers or slices, not str" ERROR

When trying to convert a JSON file to CSV, I get the following error: TypeError: list indices must be integers or slices, not str

TypeError: list indices must be integers or slices, not str - JSON, Python error

list indices must be integers or slices, not str error Python

Error "list indices must be integers or slices, not str" while looping

error list indices must be integers or slices, not str in flask

Python Error : list indices must be integers or slices, not str

Error TypeError: list indices must be integers or slices, not str

how to solve TypeError: list indices must be integers or slices, not str in python

How to solve "TypeError: list indices must be integers or slices, not str"

TypeError: list indices must be integers or slices, not str , dont know what im doing wrong

list indices must be integers or slices, not str error when trying to extract data from json file

Pulling data from JSON in Python: error - "list indices must be integers or slices, not str"

Python: Can't replace item inside list due to: TypeError: list indices must be integers or slices, not str

"TypeError: list indices must be integers or slices, not str" When i trying to read json

Why am I getting TypeError list indices must be integers or slices , not str while passing arguments to multiprocessing pool?

I run the code of json they show me ' TypeError: list indices must be integers or slices, not str'

TypeError: list indices must be integers or slices, not str when I try to iterate lists of strings with special characters inside some of them

TypeError: list indices must be integers or slices, not str

TypeError: list indices must be integers or slices not str