Conditional operation on Pandas DataFrame columns
Suppose you have an online store. The price of the products is updated frequently. While calculating the final price on the product, you check if the updated price is available or not. If not available then you use the last price available.
Solution #1: We can use conditional expression to check if the column is present or not. If it is not present then we calculate the price using the alternative column.
# importing pandas as pd import pandas as pd # Create the dataframe df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Product':['Umbrella', 'Matress', 'Badminton', 'Shuttle'], 'Last Price':[1200, 1500, 1600, 352], 'Updated Price':[1250, 1450, 1550, 400], 'Discount':[10, 10, 10, 10]}) # Print the dataframe print(df) |
Output :

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.
# Check if the updated price is available or not if 'Updated Price' in df.columns: df['Final cost'] = df['Updated Price'] - (df['Updated Price']*0.1) else : df['Final cost'] = df['Last Price'] - (df['Last Price']*0.1) # Print the Dataframe print(df) |
Output :

As we can see in the output, as the ‘Update 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.
# importing pandas as pd import pandas as pd # Create the dataframe df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Product':['Umbrella', 'Matress', 'Badminton', 'Shuttle'], 'Last Price':[1200, 1500, 1600, 352], 'Discount':[10, 10, 10, 10]}) # Print the dataframe print(df) |
Output :

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.
# Check if the updated price is available or not if 'Updated Price' in df.columns: df['Final cost'] = df['Updated Price'] - (df['Updated Price']*0.1) else : df['Final cost'] = df['Last Price'] - (df['Last Price']*0.1) # Print the Dataframe print(df) |
Output :

Solution #2: We can use Python’s issubset() function to check if the desired columns are present in the set or not.
# importing pandas as pd import pandas as pd # Create the dataframe df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Product':['Umbrella', 'Matress', 'Badminton', 'Shuttle'], 'Last Price':[1200, 1500, 1600, 352], 'Updated Price':[1250, 1450, 1550, 400], 'Discount':[10, 10, 10, 10]}) # Print the dataframe print(df) |
Output :

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.
# Check if the updated price is available or not if {'Updated Price', 'Discount'}.issubset(df.columns): df['Final cost'] = df['Updated Price'] - (df['Updated Price']*0.1) elif {'Last Price', 'Discount'}.issubset(df.columns): df['Final cost'] = df['Last Price'] - (df['Last Price']*0.1) # Print the Dataframe print(df) |
Output :

As we can see in the output, as the ‘Update 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.
# importing pandas as pd import pandas as pd # Create the dataframe df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Product':['Umbrella', 'Matress', 'Badminton', 'Shuttle'], 'Last Price':[1200, 1500, 1600, 352], 'Discount':[10, 10, 10, 10]}) # Print the dataframe print(df) |
Output :

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.
# Check if the updated price is available or not if {'Updated Price', 'Discount'}.issubset(df.columns): df['Final cost'] = df['Updated Price'] - (df['Updated Price']*0.1) elif {'Last Price', 'Discount'}.issubset(df.columns): df['Final cost'] = df['Last Price'] - (df['Last Price']*0.1) # Print the Dataframe print(df) |
Output :

As we can see in the output, as the ‘Update Price’ column was not available, so the ‘Final Cost’ has been calculated on the basis of last price.
Recommended Posts:
- Python | Pandas DataFrame.columns
- How to rename columns in Pandas DataFrame
- Difference of two columns in Pandas dataframe
- How to select multiple columns in a pandas dataframe
- Iterating over rows and columns in Pandas DataFrame
- How to drop one or multiple columns in Pandas Dataframe
- Getting frequency counts of a columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Using dictionary to remap values in Pandas DataFrame columns
- Split a text column into two columns in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Create a new column in Pandas DataFrame based on the existing columns
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



