Home > Back-end >  Python Dataframe subtract a value from each list of a row
Python Dataframe subtract a value from each list of a row

Time:05-02

I have a data frame consisting of lists as elements. I want to subtract a value from each list and create a new column. My code:

df = pd.DataFrame({'A':[[1,2],[4,5,6]]})
df
           A
0     [1, 2]
1  [4, 5, 6]

# lets substract 1 from each list
val = 1
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x[0],[val]*len(x[0]))],axis=1)

Present solution:

IndexError: index 3 is out of bounds for axis 0 with size 2

Expected solution:

df
           A      A_new
0     [1, 2]     [0, 1]
1  [4, 5, 6]  [3, 4, 5]

CodePudding user response:

Convert to numpy array

df['A_new'] = df.A.map(np.array)-1
Out[455]: 
0       [0, 1]
1    [3, 4, 5]
Name: A, dtype: object

CodePudding user response:

df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x,[val]*len(x))])

You have to pass the list to the len function. Here x is the list itself. So indexing it, x[0] just returns a number which is wrong given the context. This gives the output:

           A      A_new
0     [1, 2]     [0, 1]
1  [4, 5, 6]  [3, 4, 5]

CodePudding user response:

How about a simple list comprehension:

df['new'] = [[i - 1 for i in l] for l in df['A']]

           A        new
0     [1, 2]     [0, 1]
1  [4, 5, 6]  [3, 4, 5]

CodePudding user response:

You can convert the list to np.array and then subtract the val:

import numpy as np

df['A_new'] = df['A'].apply(lambda x: np.array(x) - val)

Output:

           A      A_new
0     [1, 2]     [0, 1]
1  [4, 5, 6]  [3, 4, 5]
  • Related