Reputation: 65
I have a pandas dataframe, 'df', like the following:
name things_in_bag
0 Don [orange, pear, apple]
1 Tommy [banana, pear, apple, watermelon]
2 Larry [cucumber]
.
.
1084 Jen [pear, baseball]
I want to add a column called 'number_things' that totals the things in the bag.
I know I can use len() to get the count of the items in those arrays, however, the problem is accessing the index. So the number of items in the first row is len(df.loc[0, 'things_in_bag']).
But how do I populate all 1084 rows of the new column?
Upvotes: 1
Views: 252
Reputation: 17152
you can use apply and look at the len of the list such as
df['things_in_bag'].apply(len)
if you want to populate a new columns with that data you can then do
df['newcol'] = df['things_in_bag'].apply(len)
Upvotes: 1