Sum values in a list contained in every row of a column pandas dataframe

Viewed 336

I have a df where every row in the column "numbers" is a list of floats. I want to add a column to the df with the sum of those floats.

#current output
letter   numbers
a        [0.0, 0.1, 2.3]
b        [5, 6.7, 11.21] 

#desired output
letter   numbers           sum_result
a        [0.0, 0.1, 2.3]   2.4
b        [5, 6.7, 11.21]   22.91  

I have tried sum(df.numbers) and get this error message

TypeError: unsupported operand type(s) for +: 'int' and 'list'

Any help would be appreciated!

1 Answers

Use Series.apply with sum:

df['sum_result'] = df['numbers'].apply(sum)

Or list comprehension:

df['sum_result'] = [sum(x) for x in df['numbers']]
Related