Column Operations in Pandas

Say I have a data frame:

import numpy as np import pandas as pd df = pd.DataFrame(np.random.rand(4,5), columns = list('abcde')) 

I would like to subtract entries from the df.a column from all other columns. In other words, I would like to get a data frame that contains the following columns as columns:

| col_b - col_a | col_c - col_a | col_d - col_a |

I tried df - df.a , but this gives something odd:

  0 1 2 3 abcde 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 perform this type of column operation in Pandas? Also, just wondering what df -df.a ?

+4
source share
1 answer

You probably want

 >>> df.sub(df.a, axis=0) abcde 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 basically tries to perform subtraction on the other axis, so the indices do not match, and when using binary operators such as subtraction, “inconsistent indices will be merged together” (as the docs say). Since the indices do not match, you end with 0 1 2 3 abcde .

For example, you could get an indirect indirect move, (df.T - df.a).T , which, turning df over, means that the default axis is now correct.

+6
source

All Articles