Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract pandas columns from a specified column

How can I dynamically subtract values in multiple pandas dataframe columns from a specified column. In this case, how can I subtract columns A, B, and C from deposit and place the value in the corresponding A, B, and C columns.

   date         deposit       A                 B           C
0  2017-01-15   12            5                 10          12
1  2017-01-16   20            10                4           32
2  2017-01-17   5             50                10          18
3  2017-01-18   22            15                20          12

should produce:

   date         deposit       A                 B           C
0  2017-01-15   12            7                 2           0
1  2017-01-16   20            10                16         -12
2  2017-01-17   5            -45               -5          -13
3  2017-01-18   22            7                 2           10
like image 268
DougKruger Avatar asked May 03 '17 21:05

DougKruger


People also ask

How do I subtract two columns in pandas?

We can create a function specifically for subtracting the columns, by taking column data as arguments and then using the apply method to apply it to all the data points throughout the column.

How do you subtract two data sets in pandas?

subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs.

How do I subtract one panda's DataFrame from another?

The sub() method of pandas DataFrame subtracts the elements of one DataFrame from the elements of another DataFrame. Invoking sub() method on a DataFrame object is equivalent to calling the binary subtraction operator(-). The sub() method supports passing a parameter for missing values(np. nan, None).


3 Answers

In [226]: df[['A','B','C']] = df.deposit.values[:, None] - df[['A','B','C']]

In [227]: df
Out[227]:
         date  deposit   A   B   C
0  2017-01-15       12   7   2   0
1  2017-01-16       20  10  16 -12
2  2017-01-17        5 -45  -5 -13
3  2017-01-18       22   7   2  10
like image 82
MaxU - stop WAR against UA Avatar answered Oct 08 '22 12:10

MaxU - stop WAR against UA


loc + rsub

cols = ['A', 'B', 'C']
df.loc[:, cols] = df[cols].rsub(df.deposit, 0)
df


         date  deposit   A   B   C
0  2017-01-15       12   7   2   0
1  2017-01-16       20  10  16 -12
2  2017-01-17        5 -45  -5 -13
3  2017-01-18       22   7   2  10

inplace
My preference for doing it inplace

df.update(df[['A', 'B', 'C']].rsub(df.deposit, 0))

df

         date  deposit   A   B   C
0  2017-01-15       12   7   2   0
1  2017-01-16       20  10  16 -12
2  2017-01-17        5 -45  -5 -13
3  2017-01-18       22   7   2  10

copy
My preference overall

df.assign(**df[['A', 'B', 'C']].rsub(df.deposit, 0).to_dict('list'))

         date  deposit   A   B   C
0  2017-01-15       12   7   2   0
1  2017-01-16       20  10  16 -12
2  2017-01-17        5 -45  -5 -13
3  2017-01-18       22   7   2  10
like image 34
piRSquared Avatar answered Oct 08 '22 14:10

piRSquared


for c in ['A','B','C']:
    df[c]=df['deposit']-df[c]
like image 34
alex314159 Avatar answered Oct 08 '22 12:10

alex314159