How to Subtract Two Columns in Pandas DataFrame?


While working with Pandas DataFrames, situations may arise where arithmetic operations between attributes are necessary. One such operation is deducting two attributes. In this guide, we will delve into three distinct techniques to deduct two attributes in a Pandas DataFrame: employing the `sub` method, utilizing the `apply` method combined with a lambda function, and leveraging the `subtract` function. Examples will aid in understanding these approaches.

Method 1: Employing the `sub` method

The `sub` method is an intrinsic Pandas function that facilitates direct deduction of one attribute from another. This technique is straightforward and effective for performing deductions between DataFrame attributes.

Syntax

result = dataframe['attribute1'].sub(dataframe['attribute2'])

In this scenario, 'attribute1' and 'attribute2' symbolize the names of attributes within the DataFrame, yielding a Pandas Arrangement that encapsulates the discovery of these two attributes.

Example

In the given instance, a DataFrame is constructed, comprising two attributes, namely 'A' and 'B'.

We then deduct attribute 'B' from attribute 'A' utilizing the `sub` method and store the outcome in a new attribute named 'Difference'.

import pandas as pd

data = {'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35]}
df = pd.DataFrame(data)

df['Difference'] = df['A'].sub(df['B'])
print(df)

Output

    A   B  Difference
0  10   5           5
1  20  15           5
2  30  25           5
3  40  35           5

Method 2: Utilizing the `apply` method with a lambda function

The `apply` method, when combined with a lambda function, enables element-wise deduction between two attributes in a DataFrame.

Syntax

result = dataframe.apply(lambda row: row['attribute1'] - row['attribute2'], axis=1)

Here, the `apply` method iterates across the DataFrame's rows. The lambda function accepts each row as input and deducts 'attribute2' from 'attribute1'. The result is a Pandas Series containing the deduction of the two attributes.

Example

In the subsequent example, we use the same DataFrame as before and deduct attribute 'B' from attribute 'A' employing the `apply` method with a lambda function. The outcome is stored in a new attribute called 'Difference'.

import pandas as pd

data = {'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35]}
df = pd.DataFrame(data)

df['Difference'] = df.apply(lambda row: row['A'] - row['B'], axis=1)
print(df)

Output

    A   B  Difference
0  10   5           5
1  20  15           5
2  30  25           5
3  40  35           5

Method 3: Leveraging the `subtract` function

The `subtract` function is another intrinsic Pandas function that allows for deductions between DataFrame attributes.

Syntax

result = dataframe['attribute1'].subtract(dataframe['attribute2'])

In this context, 'attribute1' and 'attribute2' stand as placeholders for attribute names within the DataFrame, culminating in a Pandas Arrangement that encompasses the outcome of these two attributes.

Example

Within the consequent illustration, we utilize the same DataFrame as before and deduct attribute 'B' from attribute 'A' utilizing the `subtract` function. The outcome is stored in a new attribute named 'Difference'.

import pandas as pd

data = {'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35]}
df = pd.DataFrame(data)

df['Difference'] = df['A'].subtract(df['B'])
print(df)

Output

    A   B  Difference
0  10   5           5
1  20  15           5
2  30  25           5
3  40  35           5

Conclusion

In this guide, we examined how to deduct two attributes in a Pandas DataFrame utilizing various techniques. We explored the `sub` method, the `apply` method combined with a lambda function, and the `subtract` function. Each technique offers a simple and effective means to perform attribute-wise deductions in Pandas DataFrames, depending on the specific needs of your data manipulation tasks.

Updated on: 28-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements