Python | Creating a Pandas dataframe column based on a given condition
While operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to achieve this task directly, so we are going to see the ways in which we can achieve this goal.
Problem : Given a dataframe containing the data of a cultural event, add a column called ‘Price’ which contains the ticket price for a particular day based on the type of event that will be conducted on that particular day.
Solution #1 : We can use Python’s list comprehension technique to achieve this task. List comprehension is mostly faster than other methods.
# importing pandas as pd import pandas as pd # Creating the dataframe df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', '11/11/2011', '11/12/2011'], 'Event' : ['Music', 'Poetry', 'Music', 'Music', 'Poetry']}) # Print the dataframe print(df) |
Output :

Now we will add a new column called ‘Price’ to the dataframe. For that purpose, we will use list comprehension technique. Set the price to 1500 if the ‘Event’ is ‘Music’ else 800.
# Add a new column named 'Price' df['Price'] = [1500 if x =='Music' else 800 for x in df['Event']] # Print the DataFrame print(df) |
Output :

As we can see in the output, we have successfully added a new column to the dataframe based on some condition.
Solution #2 : We can use DataFrame.apply() function to achieve the goal. There could be instances when we have more than two values, in that case, we can use a dictionary to map new values onto the keys. This does provide a lot of flexibility when we are having a larger number of categories for which we want to assign different values to the newly added column.
# importing pandas as pd import pandas as pd # Creating the dataframe df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', '11/11/2011', '11/12/2011'], 'Event' : ['Music', 'Poetry', 'Music', 'Comedy', 'Poetry']}) # Print the dataframe print(df) |
Output :

Now we will add a new column called ‘Price’ to the dataframe. For that purpose we will use DataFrame.apply() function to achieve the goal. Set the price to 1500 if the ‘Event’ is ‘Music’, 1200 if the ‘Event’ is ‘Comedy’ and 800 if the ‘Event’ is ‘Poetry’.
# Define a function to map the values def set_value(row_number, assigned_value): return assigned_value[row_number] # Create the dictionary event_dictionary ={'Music' : 1500, 'Poetry' : 800, 'Comedy' : 1200} # Add a new column named 'Price' df['Price'] = df['Event'].apply(set_value, args =(event_dictionary, )) # Print the DataFrame print(df) |
Output :

As we can see in the output, we have successfully added a new column to the dataframe based on some condition.
Solution #3 : We can use DataFrame.map() function to achieve the goal. It is a very straight forward method where we use a dictionary to simply map values to the newly added column based on the key.
# importing pandas as pd import pandas as pd # Creating the dataframe df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011', '11/11/2011', '11/12/2011'], 'Event' : ['Music', 'Poetry', 'Music', 'Comedy', 'Poetry']}) # Print the dataframe print(df) |
Output :

Now we will add a new column called ‘Price’ to the dataframe. For that purpose we will use DataFrame.map() function to achieve the goal. Set the price to 1500 if the ‘Event’ is ‘Music’, 1200 if the ‘Event’ is ‘Comedy’ and 800 if the ‘Event’ is ‘Poetry’.
# Create the dictionary event_dictionary ={'Music' : 1500, 'Poetry' : 800, 'Comedy' : 1200} # Add a new column named 'Price' df['Price'] = df['Event'].map(event_dictionary) # Print the DataFrame print(df) |
Output :

Recommended Posts:
- Drop rows from the dataframe based on certain condition applied on a column
- Return the Index label if some condition is satisfied over a column in Pandas Dataframe
- Create a new column in Pandas DataFrame based on the existing columns
- Python | Change column names and row indexes in Pandas DataFrame
- Creating a Pandas DataFrame
- Creating a dataframe from Pandas series
- Creating views on Pandas DataFrame | Set - 2
- Creating views on Pandas DataFrame
- Creating a Pandas dataframe using list of tuples
- Creating Pandas dataframe using list of lists
- How to get column names in Pandas dataframe
- Get unique values from a column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- Create a column using for loop in Pandas Dataframe
- Get n-smallest values from a particular column 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.



