简体   繁体   中英

TypeError: argument of type 'int' is not iterable (using lambda)

在此处输入图片说明

在此处输入图片说明

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

I have this :

TypeError: argument of type 'int' is not iterable

how can I fix it ?

There is problem one, multiple or all values are integers in column InvoiceNo .

Possible solution if mixed column values ( C are in another values not shown in sample data) is cast it to string s by astype :

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

Another solution with str.contains :

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

If all values are integers there are no C , so always get 0 .

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