Search code examples
pythonpandasapplydata-munging

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


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


Solution

  • 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]