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

Time:02-24

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, i.e.

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.

CodePudding user response:

Try with explode then groupby with sum

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

CodePudding user response:

You can use df.apply()

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