receiving indexerror: string index out of range when using apply

  • Last Update :
  • Techknowledgy :

This is my code

import pandas as pd
import numpy as np
import nltk

train = pd.read_csv(r 'C:\Users\JKC\Downloads\classification_train.csv', names = ['product_title', 'brand_id', 'category_id'])

train['product_title'] = train['product_title'].apply(lambda x: x.lower())

def preprocessing(x):
   tokens = nltk.pos_tag(x.split(" "))
list = []
for y, x in tokens:
   if (x == "NN"
      or x == "NNS"
      or x == "NNP"
      or x == "NNPS"):
      list.append(y)
return (' '.join(list))
# My
function works fine
if I use preprocessing(train['product_title'][1])

train['token'] = train['product_title'].apply(preprocessing, 1)

There are no empty rows in my data:

train.isnull().sum()
Out[12]:
   product_title 0
brand_id 0
category_id 0
dtype: int64

Suggestion : 2

Remote Work 2022 , Remote Work 2022

Syntax of range()

range(start, stop, step)

Code to fix list out of range error:

list1 = ["Hire", "the", "top", 10, "python", "freelancers"]

for i in range(0, len(list1)):
   print(list1[i])

Although the above method solves the ‘list index out of range’ error, another caveat while dealing with list indexes is as follows. Programmers often use the wrong variable to index a list. This example would give you a better understanding.

list1 = [5, 10, 15, 20, 25]

for i in list1:
   print(list1[i])

Suggestion : 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL),Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. ,If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome., communitylounge Who's Who Most Valuable Professionals The Lounge   The CodeProject Blog Where I Am: Member Photos The Insider News The Weird & The Wonderful

n = int(input())
L = len(n)
i = 0
While(i
1._
n = int(input())
L = len(n)
i = 0
while (i < L)
   print("ABCD" [i])
   ...
2._
0, 1, 2, 3

Suggestion : 4

Generally, list index out of range means means that you are providing an index for which a list element does not exist.,Now, for this exercise they require you to create a generic function for an unknown number of strings in a list(i.e. any amount of strings in a list) not just 2 strings as provided in the exercise. Your function would not work if there were, let’s say, 3 strings in the list. ,Therefore this exercise requires you to create a for loop, to loop through each string in absolutely any list provided. The n = ["Michael", "Lieberman"] list was just provided for testing purposes; not to create a function based around that list. ,Since nobody has answered this, and you’ve probably passed this exercise, i’ll still answer for other people to see lol.

I’ll print my code below, it prints what I think should be the correct answer which is: MichaelLieberman None However, I am getting the error “Oops, try again! Your function caused the following error: list index out of range“ but i’m not sure why. Also, why if i’m getting the correct output am I not able to actually pass this level?

n = ["Michael", "Lieberman"]
# Add your
function here

def join_strings(x):
   return x[0] + x[1]

print join_strings(n)

Not sure why you got the error. I didn’t face this:

>>> n = ['a', 'b'] >>>
   def joy(x):
   ...
   return x[0] + x[1]
      ...
      >>>
      print joy(n)
ab
   >>>
   n = ['a', 'b', 'c'] >>>
   print joy(n)
ab
   >>>
   n = ['Michael', 'Lieberman'] >>>
   print joy(n)
MichaelLieberman
   >>>
   n = ['Michael', 'Lieberman', 'Python'] >>>
   print joy(n)
MichaelLieberman
   >>>