Reputation: 101
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.
Upvotes: 0
Views: 24
Reputation: 9061
You can use df.apply()
df['COUNT'] = df['STRUCTURE'].apply(lambda x: sum(e >= 10 for e in x))
Upvotes: 0
Reputation: 323306
Try with explode
then groupby
with sum
df['new'] = df.STRUCTURE.explode().ge(10).groupby(level=0).sum()
Upvotes: 1