Wessi
Wessi

Reputation: 1802

Pandas: sum values two columns consisting of lists

I have a Pandas dataframe looking like this.

id       discount          current_price
01188    [0, 0, 0, 78]     [0, 294, 294, 294, 294, 294, 294, 294, 294, 294]

The column discount and price are both consist of a list of integers. I would like to create a new column called old_price with the sum of discount and price element-wise.

So the new column would have: [0, 294, 294, 372, 294, 294, 294, 294, 294, 294]

Upvotes: 0

Views: 487

Answers (1)

user3483203
user3483203

Reputation: 51165

I would use a good old list-comprehension here with itertools.zip_longest. This is probably the fastest you're going to get if you're set on using lists in a DataFrame.


from itertools import zip_longest

df.assign(old_price=[
    [x + y for x, y in zip_longest(d, c, fillvalue=0)]
    for d, c in zip(df.discount, df.current_price)
])

     id       discount                                     current_price                                         old_price
0  1188  [0, 0, 0, 78]  [0, 294, 294, 294, 294, 294, 294, 294, 294, 294]  [0, 294, 294, 372, 294, 294, 294, 294, 294, 294]

If all of your rows are the same length, you can probably optimize how your data is stored, but again, that's data dependent.

Upvotes: 4

Related Questions