Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column operations in Pandas

Tags:

python

pandas

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?

like image 584
Amelio Vazquez-Reina Avatar asked Feb 19 '13 03:02

Amelio Vazquez-Reina


People also ask

How do I run between two columns in pandas?

Difference of two columns in pandas dataframe in Python is carried out by using following methods : Method #1 : Using ” -” operator. Method #2 : Using sub() method of the Dataframe.

How do I apply a function to all columns in pandas?

Use apply() to Apply Functions to Columns in Pandas The apply() method allows to apply a function for a whole DataFrame, either across columns or rows. We set the parameter axis as 0 for rows and 1 for columns. The new appended e column is the sum of data in column a and b .


1 Answers

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.

like image 181
DSM Avatar answered Sep 27 '22 22:09

DSM