Subtract a Series from a DataFrame while keeping the DataFrame struct intact

dataframe, numpy, pandas, python

Solution

Maybe:

>>> df = pd.DataFrame(np.zeros((5,3)))
>>> s = pd.Series(np.ones(5))
>>> df.sub(s,axis=0)
   0  1  2
0 -1 -1 -1
1 -1 -1 -1
2 -1 -1 -1
3 -1 -1 -1
4 -1 -1 -1

[5 rows x 3 columns]

or, for a more interesting example:

>>> s = pd.Series(np.arange(5))
>>> df.sub(s,axis=0)
   0  1  2
0  0  0  0
1 -1 -1 -1
2 -2 -2 -2
3 -3 -3 -3
4 -4 -4 -4

[5 rows x 3 columns]

Problem

How can I subtract a Series from a DataFrame, while keeping the DataFrame struct intact? ``` df = pd.DataFrame(np.zeros((5,3))) s = pd.Series(np.ones(5)) df - s 0 1 2 3 4 0 -1 -1 -1 NaN NaN 1 -1 -1 -1 NaN NaN 2 -1 -1 -1 NaN NaN 3 -1 -1 -1 NaN NaN 4 -1 -1 -1 NaN NaN ``` What I would like to have is the equivalent of subtracting a scalar from the DataFrame ``` df - 1 0 1 2 0 -1 -1 -1 1 -1 -1 -1 2 -1 -1 -1 3 -1 -1 -1 4 -1 -1 -1 ```

Original source