numpy - Subtract two columns of lists in pandas -


i have dataframe 2 columns of 1d lists of same size, , form third column difference of these vectors. conceptually:

df['dv'] = df['v1'] - df['v2'] 

so if df['v1'] looks like:

0  [0.2, 0.1, 0.0] 1  [0.5, -0.4, 0.0] ... 

and df['v2'] looks like:

0  [0.1, 0.6, 0.0] 1  [0.5, 0.4, 0.0] ... 

then desired result df['dv'] be:

0  [0.1, -0.5, 0.0] 1  [0.0, -0.8, 0.0] ... 

i have tried following:

df['dv'] = df['v1'] - df['v2'] 

which results in "operands not broadcast.." error. next, tried:

vecsub = lambda x, y: np.subtract(x, y) df['dv'] = list(map(vecsub, df['v1'], df['v2'])) 

this produces result, types different:

type(df['dv']) 

is numpy.ndarray

while

type(df['v1']) 

is list.

how might results in dv lists? applying numpy's tolist around lambda outputs <built-in method tolist of numpy.ndarray object> every value in dataframe.

if want change ndarray list list(df['dv']) broadcasting errors happen when arrays have different size. sure shapes equal? can use .shape information. can read more broadcasting here.

applying numpy's tolist around lambda outputs <built-in method tolist of numpy.ndarray object> every value in dataframe.

thats because did: somearray.tolist, instead of somearray.tolist(), printing function, not calling , printing it's result.


Comments