While working with data in Pandas, we perform a vast array of operations on the data to get the data in the desired form. One of these operations could be that we want to create new columns in the DataFrame based on the result of some operations on the existing columns in the DataFrame. Let’s discuss several ways in which we can do that.
Given a Dataframe containing data about an event, we would like to create a new column called ‘Discounted_Price’, which is calculated after applying a discount of 10% on the Ticket price.
Solution #1: We can use DataFrame.apply() function to achieve this task.
# importing pandas as pd import pandas as pd # Creating the DataFrame df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Event':['Music', 'Poetry', 'Theatre', 'Comedy'], 'Cost':[10000, 5000, 15000, 2000]}) # Print the dataframe print(df) |
Output :

Now we will create a new column called ‘Discounted_Price’ after applying a 10% discount on the existing ‘Cost’ column.
# using apply function to create a new column df['Discounted_Price'] = df.apply(lambda row: row.Cost - (row.Cost * 0.1), axis = 1) # Print the DataFrame after addition # of new column print(df) |
Output :

Solution #2: We can achieve the same result by directly performing the required operation on the desired column element-wise.
# importing pandas as pd import pandas as pd # Creating the DataFrame df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 'Event':['Music', 'Poetry', 'Theatre', 'Comedy'], 'Cost':[10000, 5000, 15000, 2000]}) # Create a new column 'Discounted_Price' after applying # 10% discount on the existing 'Cost' column. # create a new column df['Discounted_Price'] = df['Cost'] - (0.1 * df['Cost']) # Print the DataFrame after # addition of new column print(df) |
Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Adding new column to existing DataFrame in Pandas
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- Sort rows or columns in Pandas Dataframe based on values
- Split a text column into two columns in Pandas DataFrame
- Select all columns, except one given column in a Pandas DataFrame
- How to add one row in an existing Pandas DataFrame?
- Python | Creating a Pandas dataframe column based on a given condition
- How to Sort a Pandas DataFrame based on column names or row index?
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Get column index from column name of a given Pandas DataFrame
- How to create an empty DataFrame and append rows & columns to it in Pandas?
- Find duplicate rows in a Dataframe based on all or selected columns
- Create a column using for loop in Pandas Dataframe
- Drop rows from the dataframe based on certain condition applied on a column
- How to select rows from a dataframe based on column values ?
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to select multiple columns in a pandas dataframe
- How to drop one or multiple columns in Pandas Dataframe
- How to rename columns in Pandas 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.

