简体   繁体   中英

Summing columns in a Panda's dataframe where each cell value is a list

I have a single column Dataframe similar to the example below:

import pandas
df = pandas.DataFrame({'Column': [[10, 100],[20, 200],[30, 300]]})

     Column
0 [10, 100]
1 [20, 200]
2 [30, 300]

How can I add the rows element by element such that the result will be:

     Column
0 [60, 600]

Assuming all the arrays have the same size as in the sample data:

np.array(df['Column'].to_list()).sum(axis=0)

Output:

array([ 60, 600])

I would recommend the apply function. Try it this way:

Import pandas as pd
df = pd.DataFrame({'Column': [[10, 100],[20, 200],[30, 300]]})
nums = df.Column.apply(pd.Series)
nums.sum()

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