Column operations in Pandas
pandas, python
Solution
You probably want
>>> df.sub(df.a, axis=0)
a b c d e
0 0 0.112285 0.267105 0.365407 -0.159907
1 0 0.380421 0.119536 0.356203 0.096637
2 0 -0.100310 -0.180927 0.112677 0.260202
3 0 0.653642 0.566408 0.086720 0.256536
`df-df.a` is basically trying to do the subtraction along the other axis, so the indices don't match, and when using binary operators like subtraction "mismatched indices will be unioned together" (as the docs say). Since the indices don't match, you wind up with `0 1 2 3 a b c d e`.
For example, you could get to the same destination more indirectly by transposing things, `(df.T - df.a).T`, which by flipping `df` means that the default axis is now the right one.
Problem
Say I have a dataframe: ``` import numpy as np import pandas as pd df = pd.DataFrame(np.random.rand(4,5), columns = list('abcde')) ``` I would like to substract the entries in column `df.a` from all other columns. In other words, I would like to get a dataframe that holds as columns the following columns: |`col_b - col_a` | `col_c - col_a` | `col_d - col_a`| I have tried `df - df.a` but this yields something odd: ``` 0 1 2 3 a b c d e 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN ``` How can I do this type of columnwise operations in Pandas? Also, just wondering, what does `df -df.a` do?