Home >Backend Development >Python Tutorial >How Can I Create a New Column in Pandas Based on Values from an Existing Column?

How Can I Create a New Column in Pandas Based on Values from an Existing Column?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-27 21:14:14676browse

How Can I Create a New Column in Pandas Based on Values from an Existing Column?

Create a New Column with Values Selected from an Existing Column

Selecting particular values from an existing column to populate a new column is a common data manipulation task. Let's explore two approaches to achieve this in Python using Pandas.

Method 1: np.where for Two Choices

When there are only two options to choose from, the np.where function is a convenient choice. In the example provided, we want to create a color column with 'green' for values in the Set column equal to 'Z' and 'red' otherwise.

df['color'] = np.where(df['Set']=='Z', 'green', 'red')

Method 2: np.select for Multiple Choices

For scenarios with more than two choices, np.select offers greater flexibility. Let's introduce more conditions for the color column:

  • 'yellow' for when (df['Set'] == 'Z') & (df['Type'] == 'A')
  • 'blue' for when (df['Set'] == 'Z') & (df['Type'] == 'B')
  • 'purple' for when (df['Type'] == 'B')
  • 'black' for all other cases

The code for this scenario is as follows:

conditions = [
    (df['Set'] == 'Z') & (df['Type'] == 'A'),
    (df['Set'] == 'Z') & (df['Type'] == 'B'),
    (df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')

This approach allows for easy customization of the conditions and values to be selected, making it suitable for more complex scenarios.

The above is the detailed content of How Can I Create a New Column in Pandas Based on Values from an Existing Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn