I think this may be easier to read and understand:
In [12]:
import numpy as np
a=pd.DataFrame(np.arange(0,20).reshape(5,4))
b=[1,2,3,4,5]
In [13]:
print (a.T-array(b)).T
0 1 2 3
0 -1 0 1 2
1 2 3 4 5
2 5 6 7 8
3 8 9 10 11
4 11 12 13 14
Or maybe this:
a-(np.zeros(a.shape)+array(b)[...,np.newaxis])
Matrix manipulation would be the slowest, the buildin .sub()
@DSM and the array broadcasting method will be similarly fast:
In [31]:
%timeit a.sub(b, axis=0)
1000 loops, best of 3: 565 us per loop
In [32]:
%timeit a-(np.zeros(a.shape)+array(b)[...,np.newaxis])
1000 loops, best of 3: 572 us per loop
In [33]:
%timeit (a.T-array(b)).T
1000 loops, best of 3: 896 us per loop
In case if you are wondering, the lambda
version is the slowest, as it often the case in python
:
In [36]:
%timeit a.apply(lambda x: x-b)
100 loops, best of 3: 2.63 ms per loop