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

Viewed 929

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)
1 Answers

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.

Related