VOOZH about

URL: https://www.geeksforgeeks.org/pandas/conditional-operation-on-pandas-dataframe-columns/

⇱ Conditional operation on Pandas DataFrame columns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Conditional operation on Pandas DataFrame columns

Last Updated : 12 Nov, 2025

Conditional operations on Pandas DataFrame columns help you modify or compute column values dynamically for example, applying different calculations depending on whether certain columns exist or meet given conditions.

Below is the Sample DataFrame used in this article:

Output

Date Product Last Price Updated Price Discount
0 10/2/2011 Umbrella 1200 1250 10
1 11/2/2011 Mattress 1500 1450 10
2 12/2/2011 Badminton 1600 1550 10
3 13/2/2011 Shuttle 352 400 10

Below are different methods to perform conditional operations on DataFrame columns.

Using a Simple Conditional Check

You can verify if a column exists in a DataFrame before performing an operation. This is useful when the dataset structure may vary.

Output

Date Product Last Price Updated Price Discount Final cost
0 10/2/2011 Umbrella 1200 1250 10 1125.0
1 11/2/2011 Mattress 1500 1450 10 1305.0
2 12/2/2011 Badminton 1600 1550 10 1395.0
3 13/2/2011 Shuttle 352 400 10 360.0

As we can see in the output, as the 'Updated Price' column was available, so the 'Final Cost' has been calculated on the updated price. Now let's consider a scenario when the 'Updated Price' is not available. 

Output

Date Product Last Price Discount Final cost
0 10/2/2011 Umbrella 1200 10 1080.0
1 11/2/2011 Mattress 1500 10 1350.0
2 12/2/2011 Badminton 1600 10 1440.0
3 13/2/2011 Shuttle 352 10 316.8

As we can see in the output, as the 'Updated Price' column was not available, so the 'Final Cost' has been calculated on the basis of last price.

Using issubset() for Multiple Column Checks

If you want to ensure multiple required columns exist before performing an operation, you can use Python’s issubset() function.

Now we will check if the updated price is available or not. If not available, then we will apply the discount of 10% on the 'Last Price' column to calculate the final price. 

Output

Date Product Last Price Updated Price Discount Final cost
0 10/2/2011 Umbrella 1200 1250 10 1125.0
1 11/2/2011 Mattress 1500 1450 10 1305.0
2 12/2/2011 Badminton 1600 1550 10 395.0
3 13/2/2011 Shuttle 352 400 10 360.0

As we can see in the output, as the 'Updated Price' column was available, so the 'Final Cost' has been calculated on the updated price. Now let's consider a scenario when the 'Updated Price' is not available. 

Output

Date Product Last Price Discount Final cost

0 10/2/2011 Umbrella 1200 10 1080.0
1 11/2/2011 Mattress 1500 10 1350.0
2 12/2/2011 Badminton 1600 10 1440.0
3 13/2/2011 Shuttle 352 10 316.8

As we can see in the output, as the 'Updated Price' column was not available, so the 'Final Cost' has been calculated on the basis of last price.

Related Articles:

Comment

Explore