Skip to content
Advertisement

How do I operate (subtract) a Series with a DataFrame for every column?

How do I operate on a DataFrame with a Series for every column?

And for the reverse operation (Series – DataFrame)?

df = pd.DataFrame({0: [1,2,3], 1: [2,3,4]})
s0 = pd.Series([1,1,1])
   df              s0
   0  1          0  1
0  1  2          1  1
1  2  3          2  1
2  3  4          dtype: int64

I want s0-df.

   0  1
0  0 -1
1 -1 -2
2 -2 -3

My first inelegants solutions: -df.sub(s0, axis=0) ou (-df).add(s0, axis=0) !

An another idea?

Advertisement

Answer

A new idea :

df.rsub(s0, axis=0)

   0  1
0  0 -1
1 -1 -2
2 -2 -3
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement