1

I want to input language code using Google API. I ran the below code, however all values in the language column input the same. It seems the final line had an error because the values ​​of all rows are being updated with the code value of the last row. How can I correct this code?

for row in row_list:
    for column in column_list:
        detectText = str(df02.loc[row, column]).strip()
        if detectText != 'nan':
            print(df02.loc[row, column])
            detect_text = detect_language(detectText)
            df02.loc[row]['language'] = detect_text

Below is my expected output

enter image description here

purplecollar
  • 176
  • 1
  • 14

2 Answers2

3

First dont use loops if exist some alternatives, e.g.:

df02['language']  = df02['Text'].dropna().astype(str).str.strip().apply(detect_language)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

I switched to the following code, also expected output came out.

df02.loc[row, 'language'] = detect_text
purplecollar
  • 176
  • 1
  • 14