简体   繁体   中英

TypeError: object of type 'generator' has no len()

How could i fix this error: TypeError: object of type 'generator' has no len()

Thanks!

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)

You can't use len on df.iterrows , because it returns a generator - an object that returns one value at a time, and has no length (unlike Python sequences).

You can use len(df.index) instead, according to this S/O answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM