当前位置: 首页 > 知识库问答 >
问题:

TypeError:类型为“generator”的对象没有len()

郤玉书
2023-03-14

如何修复此错误: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)

共有1个答案

翁翰墨
2023-03-14

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

根据这个S/O答案,您可以使用len(df.index)代替。

 类似资料: