VOOZH about

URL: https://www.geeksforgeeks.org/pandas/python-creating-a-pandas-dataframe-column-based-on-a-given-condition/

⇱ Python | Creating a Pandas dataframe column based on a given condition - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Creating a Pandas dataframe column based on a given condition

Last Updated : 30 Oct, 2025

In data analysis, it is often necessary to add a new column to a DataFrame based on specific conditions. This article demonstrates multiple methods to create a column in Pandas depending on the values of another column.

Sample DataFrame

Given a DataFrame containing details of a cultural event, add a column called Price which contains the ticket price for each day based on the type of event.

Output

Date Event
0 11/8/2011 Music
1 11/9/2011 Poetry
2 11/10/2011 Music
3 11/11/2011 Comedy
4 11/12/2011 Poetry

Method 1: Using List Comprehension

Output :

Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800

Explanation:

  • df['Event']: selects the Event column for iteration.
  • x: represents each value in the Event column.
  • x == 'Music': checks whether the current event is Music.
  • 1500 if x == 'Music' else 800: assigns 1500 for Music, otherwise 800.

Method 2: Using DataFrame.apply() with a Dictionary

Output

Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800

 Explanation:

  • set_value(event, mapping): returns the price from the dictionary for the event.
  • event_dict: maps events to prices.
  • df['Event'].apply(set_value, args=(event_dict,)): applies the function to each event.

Method 3: Using DataFrame.map()

Output

Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800

Explanation:

  • event_dict: maps each event to its corresponding price.
  • df['Event'].map(event_dict): replaces each event with its mapped price.

Method 4: Using numpy.where()

Output

Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800

Explanation:

  • df['Event'] == 'Music': checks each row in the Event column.
  • np.where(condition, value_if_true, value_if_false): assigns 1500 if condition is True, else 800.

Method 5: Using assign() Function

Output

Date Event Category
0 11/8/2011 Music Entertainment
1 11/9/2011 Poetry Literature
2 11/10/2011 Music Entertainment
3 11/11/2011 Comedy Entertainment
4 11/12/2011 Poetry Literature

Explanation:

  • df['Event'].isin(['Music', 'Comedy']): checks if each event is Music or Comedy.
  • assign(Category=lambda x: ...): creates a new column Category based on the condition.
  • condition.any(): returns True if any value in the condition is True.
  • df['Category']: stores the assigned values (Entertainment or Literature).

Method 6: Using DataFrame.loc[]

Output

Date Event Genre
0 11/8/2011 Music Rock
1 11/9/2011 Poetry Literary
2 11/10/2011 Music Rock
3 11/11/2011 Comedy Other
4 11/12/2011 Poetry Literary

Explanation:

  • df['Genre'] = 'Other': initializes the Genre column with a default value.
  • df.loc[df['Event'] == 'Music', 'Genre'] = 'Rock': sets Genre to Rock where Event is Music.
  • df['Genre']: stores the updated values based on conditions.

Method 7: Using Lambda Function

Output

Date Event Priority
0 11/8/2011 Music High
1 11/9/2011 Poetry Low
2 11/10/2011 Music High
3 11/11/2011 Comedy Low
4 11/12/2011 Poetry Low

Explanation:

  • lambda x: 'High' if x == 'Music' else 'Low': defines an anonymous function to assign High for Music, otherwise Low.
  • df['Event'].apply(...): applies the lambda function to each value in the Event column.

Related Articles:

Comment

Explore