python - 类型错误 : object of type 'generator' has no len()

标签 python

我该如何解决这个错误:
类型错误:“生成器”类型的对象没有 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)

最佳答案

您不能使用 lendf.iterrows ,因为它返回一个生成器——一个一次返回一个值的对象,并且没有长度(与 Python 序列不同)。
您可以使用 len(df.index)相反,根据 this S/O answer .

关于python - 类型错误 : object of type 'generator' has no len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67204767/

相关文章:

python - 如何在Python中使用pyqt创建饼图

python - 打印 'x' 项目数量 'y' 次?

python - 如何生成预算受限的加权随机样本,其中项目具有不同的概率和权重?

python - Sympy Subs 在更换电源时不更换符号

python - 在单个 HTML 页面上显示多个 mpld3 导出

python - 在 Python 中创建空白图像(允许逐像素操作)

Python将字符串与字符串中的动态数字匹配

Python如何准确安排一次性任务

python - 从 pandas 数据框中获取单词列表的计数,其中每列都是单词列表

Python (+SymPy) : How to get the same result as in Mathematica?