【问题标题】:TypeError: object of type 'generator' has no len()TypeError:“生成器”类型的对象没有 len()
【发布时间】:2021-07-16 04:12:00
【问题描述】:

我该如何解决这个错误: TypeError: 'generator' 类型的对象没有 len()

谢谢!

for index, row in df_result.iterrows():
if index == 0:
    tempIndex = 0
else:
    if row['LEADID'] == df_result.at[tempIndex, 'LEADID']:
        if type(row['ID']) is str:
            df_result.at[tempIndex, 'ID'] = df_result.at[tempIndex, 'ID'] + "\n" + row['ID']
        if type(row['CREATEDBYID']) is str:
            df_result.at[tempIndex, 'CREATEDBYID'] = df_result.at[tempIndex, 'CREATEDBYID'] + "\n" + row['CREATEDBYID']
        if type(row['CREATEDDATE']) is str:
            df_result.at[tempIndex, 'CREATEDDATE'] = df_result.at[tempIndex, 'CREATEDDATE'] + "\n" + row['CREATEDDATE']
        if type(row['OLDVALUE']) is str:
            df_result.at[tempIndex, row['FIELD'] + "_OLDVALUE"] = df_result.at[tempIndex, row['FIELD'] + "_OLDVALUE"] + "\n" + row['OLDVALUE']
        if type(row['NEWVALUE']) is str:
            df_result.at[tempIndex, row['FIELD'] + "_NEWVALUE"] = df_result.at[tempIndex, row['FIELD'] + "_NEWVALUE"] + "\n" + row['NEWVALUE']
    else:
        resultIndexArr.append(tempIndex)
        tempIndex = index

        if tempIndex == len(df_result.iterrows()) - 1:
            resultIndexArr.append(tempIndex)

【问题讨论】:

    标签: python


    【解决方案1】:

    您不能在df.iterrows 上使用len,因为它会返回一个生成器——一个一次返回一个值且没有长度的对象(与 Python 序列不同)。

    根据this S/O answer,您可以改用len(df.index)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 2020-02-01
      • 2019-05-23
      • 2013-04-18
      • 2015-01-21
      • 2021-12-19
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多