【问题标题】:Python: TypeError: 'numpy.int64' object is not iterablePython:TypeError:“numpy.int64”对象不可迭代
【发布时间】:2020-10-28 16:48:35
【问题描述】:

我正在尝试使用 googletrans 将文本从乌尔都语翻译成英语。当我尝试创建字典来翻译列单元格中的元素时,我收到此错误消息 TypeError: 'numpy.int64' object is not iterable。如何修复此错误?提前致谢。

    translations = {}
    for column in df_en.columns:
    # unique elements of the column
    unique_elements = df_en[column].unique()
    for element in unique_elements:
    # add translation to the dictionary
    translations[element] = translator.translate(element).text
    
    # modify all the terms of the data frame by using the previously created dictionary
    df_en.replace(translations, inplace = True)

    print(translations)

【问题讨论】:

  • 你能检查你的代码意图吗?
  • 尝试打印unique_elements
  • 哪一行引发了错误?

标签: python dictionary typeerror google-translate translate


【解决方案1】:

我之前也遇到过同样的问题。这个错误必须处理您的for 语句。您可能想尝试将代码更改为:

for column in range(unique_elements):
    #add translation to the dictionary

希望这会有所帮助!

【讨论】:

  • Neha 正在尝试运行函数“unique_element”次,但在 Python 中,您在“for 循环”中传递的参数必须是可迭代对象,因此我将其更改为“range(unique_elements )"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
相关资源
最近更新 更多