【问题标题】:TypeError: argument of type 'int' is not iterable (using lambda)TypeError:“int”类型的参数不可迭代(使用 lambda)
【发布时间】:2019-01-22 09:14:31
【问题描述】:

 nb_products_per_basket['order_canceled'] =            
 nb_products_per_basket['InvoiceNo'].apply(lambda x:int('C' in x))

我有这个:

TypeError: argument of type 'int' is not iterable

我该如何解决?

【问题讨论】:

标签: python pandas numpy


【解决方案1】:

InvoiceNo列中存在一个、多个或所有值都是整数的问题。

如果混合列值(C 位于示例数据中未显示的另一个值中)被astype 转换为strings,则可能的解决方案:

nb_products_per_basket['order_canceled'] =            
nb_products_per_basket['InvoiceNo'].astype(str).apply(lambda x:int('C' in x))

str.contains 的另一个解决方案:

nb_products_per_basket['order_canceled'] =            
nb_products_per_basket['InvoiceNo'].str.contains('C', na=False).astype(int)

如果所有值都是整数,则没有C,所以总是得到0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2011-10-04
    • 2019-08-24
    • 2020-05-07
    相关资源
    最近更新 更多