How do I create a new column where the values are selected based

ghz 7months ago ⋅ 77 views

How do I create a new column where the values are selected based on existing columns?

How do I add a color column to the following dataframe so that color='green' if Set == 'Z', and color='red' otherwise?

   Type  Set
1     A    Z
2     B    Z           
3     B    X
4     C    Y

Answers

You can achieve this using the np.where function to create a new column based on conditions from existing columns. Here's how you can do it:

import pandas as pd
import numpy as np

# Create the DataFrame
df = pd.DataFrame({'Type': ['A', 'B', 'B', 'C'],
                   'Set': ['Z', 'Z', 'X', 'Y']})

# Add a color column based on the condition
df['Color'] = np.where(df['Set'] == 'Z', 'green', 'red')

print(df)

This will produce the following DataFrame:

  Type Set  Color
0    A   Z  green
1    B   Z  green
2    B   X    red
3    C   Y    red

Explanation:

  • We use np.where(condition, value_if_true, value_if_false) to assign values to the new 'Color' column based on the condition df['Set'] == 'Z'.
  • If the condition is True, the value assigned is 'green', otherwise it's 'red'.