Home >Backend Development >Python Tutorial >How to Create a New Column with Condition-Based Values in Pandas?
Creating a New Column with Condition-Based Values
This question explores how to add a new column, color, to a given dataframe. The condition is that color should be set to 'green' if the corresponding value in the Set column is 'Z' and 'red' otherwise.
Solution with Numpy Where:
For scenarios with only two choices, the np.where method can be utilized. Here's the code:
df['color'] = np.where(df['Set'] == 'Z', 'green', 'red')
This approach effectively assigns 'green' to rows where Set is 'Z' and 'red' otherwise.
Solution with Numpy Select:
In cases where there are more than two conditions, np.select can be employed. Let's say color should meet the following criteria:
In this scenario, the code would be:
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 solution allows for flexible and granular condition-based value assignment for the new column.
The above is the detailed content of How to Create a New Column with Condition-Based Values in Pandas?. For more information, please follow other related articles on the PHP Chinese website!