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?
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With