Quantcast
Channel: Python Dataframe subtract a value from each list of a row - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Python Dataframe subtract a value from each list of a row

$
0
0

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           A0     [1, 2]1  [4, 5, 6]# lets substract 1 from each listval = 1df['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_new0     [1, 2]     [0, 1]1  [4, 5, 6]  [3, 4, 5]

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images