'Error while translating the data-frame column in pandas : IndexError: list index out of range
I am trying to translate a column of my dataframe.
Using the below code:
# Import the library:
import googletrans
from googletrans import Translator
# Create a translator object:
translator = Translator()
# Use translate method to translate the column.
df_review['eng_comments'] = df_review['comments'].apply(translator.translate, dest = 'en').apply(getattr, args=('text',))
df_review
but I am getting the error:
IndexError: list index out of range
I have checked everything and there seems no problem with indexing. Screen shot of the dataframe.
I am a beginner in coding.
Solution 1:[1]
Try checking the index of df_review['eng_comments']
column. Does it have the same index length as the comments column?
Solution 2:[2]
This can happen when you have empty rows in the column you're translating. You can just remove the empty rows and try it again.
df_review = df_review.loc[df_review['eng_comments'] != '']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hemz |
Solution 2 | Bcar |