简体   繁体   中英

Count elements in a list matching some criteria, where the list is a row in a pandas series

I have a dataframe with several columns. One of those columns contains lists, ie.

ID     CLASS     STRUCTURE
1      A         [1, 10, 15]
2      B         [3, 100, 5]
3      A         [25, 10, 1]
4      B         [100, 10000, 150]

I'd like to add a column indicating the count of elements in STRUCTURE that have a value of greater than 10, ie

ID     CLASS     STRUCTURE          COUNT
1      A         [1, 10, 15]        2
2      B         [3, 100, 5]        1
3      A         [25, 10, 1]        2
4      B         [100, 10000, 150]  3

To get a count of all numbers I can use.apply(sum) but I'm not sure how to apply within an apply, so to speak.

Try with explode then groupby with sum

df['new'] = df.STRUCTURE.explode().ge(10).groupby(level=0).sum()

You can use df.apply()

df['COUNT'] = df['STRUCTURE'].apply(lambda x: sum(e >= 10 for e in x))

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