如何通过 Python 中的 for 语句更新列值?
How to update column value via for statement in Python?
我想使用 Google API 输入语言代码。
我 运行 下面的代码,但是语言列中的所有值输入相同。
似乎最后一行有错误,因为所有行的值都在用最后一行的代码值更新。
我该如何更正此代码?
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
以下是我的预期输出
首先不要 use loops 如果存在一些替代方案,例如:
df02['language'] = df02['Text'].dropna().astype(str).str.strip().apply(detect_language)
我切换到下面的代码,同样预期输出出来了。
df02.loc[row, 'language'] = detect_text
我想使用 Google API 输入语言代码。 我 运行 下面的代码,但是语言列中的所有值输入相同。 似乎最后一行有错误,因为所有行的值都在用最后一行的代码值更新。 我该如何更正此代码?
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
以下是我的预期输出
首先不要 use loops 如果存在一些替代方案,例如:
df02['language'] = df02['Text'].dropna().astype(str).str.strip().apply(detect_language)
我切换到下面的代码,同样预期输出出来了。
df02.loc[row, 'language'] = detect_text