How to update column value via for statement in Python?

Viewed 76

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

2 Answers

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

df02['language']  = df02['Text'].dropna().astype(str).str.strip().apply(detect_language)

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

df02.loc[row, 'language'] = detect_text
Related