Home > Article > Backend Development > How to create a new column with values selected based on an existing column?
How to add the color
column to the following dataframe so that color='green'
If set == 'z'
, otherwise color='red'
?
Type Set 1 A Z 2 B Z 3 B X 4 C Y
If you only have two choices, use np.where
:
df['color'] = np.where(df['set']=='z', 'green', 'red')
For example,
import pandas as pd import numpy as np df = pd.dataframe({'type':list('abbc'), 'set':list('zzxy')}) df['color'] = np.where(df['set']=='z', 'green', 'red') print(df)
Yield
set type color 0 z a green 1 z b green 2 x b red 3 y c red
If you have more than two conditions, use np.select
. For example, if you want color
to be
yellow
When (df['set'] == 'z') & (df['type'] == 'a')
blue
When (df['set'] == 'z') & (df['type'] == 'b')
purple
when (df['type'] == 'b')
black
, Then use
df = pd.dataframe({'type':list('abbc'), 'set':list('zzxy')}) 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') print(df)
produce
Set Type color 0 Z A yellow 1 Z B blue 2 X B purple 3 Y C black
The above is the detailed content of How to create a new column with values selected based on an existing column?. For more information, please follow other related articles on the PHP Chinese website!