Pandas: each cell is a list, how to perform element-wise subtraction between cells?

Viewed 77

I have two dataframe:

df1 =   l1      l2       l3
     [4,2,1]  [8,9,6]  [7,4,5]
df2 =   l1      l2       l3
     [2,1,0]  [7,7,2]  [6,1,2]

And I want, for each cell, perform element wise substraction between df1 and df2 (For each cell and for each element, df1 - df2).

So the new_df will be:

new_df =  l1       l2       l3
        [2,1,1]  [1,2,,4] [1,3,4]

What is the most efficient way to do so?

Thanks

3 Answers

I'm not sure how you arrived at this rather clunky datastructure, but here's one clunky solution:

>>> df1.applymap(np.array) - df2
          l1         l2         l3
0  [2, 1, 1]  [1, 2, 4]  [1, 3, 3]

If there is always same number of values in each column in DataFrame use:

df1 = pd.DataFrame({'l1':[[4,2,1],[4,2,1]],'l2':[[8,9,6],[8,9,6]],'l3':[[7,4,5], [7,4,5]]})
df2 = pd.DataFrame({'l1':[[2,1,0],[2,1,0]],'l2':[[7,7,2], [2,1,0]],'l3':[[6,1,2], [2,1,0]]})

a = np.array(df1.to_numpy().tolist()) - np.array(df2.to_numpy().tolist())
df = pd.DataFrame(a.tolist(), index=df1.index, columns=df2.columns)
print (df)
          l1         l2         l3
0  [2, 1, 1]  [1, 2, 4]  [1, 3, 3]
1  [2, 1, 1]  [6, 8, 6]  [5, 3, 5]

If you want a really simple and understandable solution you can use a for loop

df1 = pd.DataFrame([[[4,2,1],[8,9,6],[7,4,5]]])
df2 = pd.DataFrame([[[2,1,0],[7,7,2],[6,1,2]]])

result = []
for lst1, lst2 in zip(df1.values[0], df2.values[0]):
    result.append([i - j for i,j in zip(lst1, lst2)])

result = pd.DataFrame([result])
``
Related