How to subtract one data frame from another?
Let me first create a scene.
I am starting with a pandas
dataframe klmn
that looks like this:
In [15]: klmn
Out[15]:
K L M N
0 0 a -1.374201 35
1 0 b 1.415697 29
2 0 a 0.233841 18
3 0 b 1.550599 30
4 0 a -0.178370 63
5 0 b -1.235956 42
6 0 a 0.088046 2
7 0 b 0.074238 84
8 1 a 0.469924 44
9 1 b 1.231064 68
10 2 a -0.979462 73
11 2 b 0.322454 97
Then I split klmn
into two data frames klmn0
and klmn1
according to the value in the "K" column:
In [16]: k0 = klmn.groupby(klmn['K'] == 0)
In [17]: klmn0, klmn1 = [klmn.ix[k0.indices[tf]] for tf in (True, False)]
In [18]: klmn0, klmn1
Out[18]:
( K L M N
0 0 a -1.374201 35
1 0 b 1.415697 29
2 0 a 0.233841 18
3 0 b 1.550599 30
4 0 a -0.178370 63
5 0 b -1.235956 42
6 0 a 0.088046 2
7 0 b 0.074238 84,
K L M N
8 1 a 0.469924 44
9 1 b 1.231064 68
10 2 a -0.979462 73
11 2 b 0.322454 97)
Finally, I calculate the average of the column M
in klmn0
, grouped by the value in the column L
:
In [19]: m0 = klmn0.groupby('L')['M'].mean(); m0
Out[19]:
L
a -0.307671
b 0.451144
Name: M
Now, my question is, how can I subtract m0
from a M
subframe column klmn1
given the value in the column L
? (By this means that it m0['a']
is subtracted from M
each row 's column klmn1
, which has 'a'
in the column L
as well as for m0['b']
.)
You can imagine this to replace the values in a column M
klmn1
with new values (after subtracting the value from m0
). Alternatively, one could do it this way to leave klmn1
unchanged and instead create a new dataframe klmn11
with the updated column M
. I am interested in both approaches.
source to share