Home >Backend Development >Python Tutorial >How Can I Create a New Column in Pandas Based on Values 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.
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')
For scenarios with more than two choices, np.select offers greater flexibility. Let's introduce more conditions for the color column:
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!