pandas new column based on condition code example

Example 1: make a condition statement on column pandas

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]

Example 2: pandas if else new column

# Method 1:
df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
#or
df.loc[df['set_of_numbers'] <= 4, 'equal_or_lower_than_4?'] = 'True' 

# Method 2:
df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')
#or
df['name_match'] = df['First_name'].apply(lambda x: 'Match' if x == 'Bill' else 'Mismatch')

# or
df.loc[(df['First_name'] == 'Bill') | (df['First_name'] == 'Emma'), 'name_match'] = 'Match'  
df.loc[(df['First_name'] != 'Bill') & (df['First_name'] != 'Emma'), 'name_match'] = 'Mismatch'

Example 3: pandas create new column conditional on other columns

# For creating new column with multiple conditions
conditions = [
    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
    (df['Base Column 3'] == 'C')]
choices = ['Conditional Value 1', 'Conditional Value 2']
df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')

Example 4: python conditionally create new column in pandas dataframe

# If you only have one condition use numpy.where()
# Example usage with np.where:
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')}) # Define df
print(df)
  Type Set
0    A   Z
1    B   Z
2    B   X
3    C   Y

# Add new column based on single condition:
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
print(df)
  Type Set  color
0    A   Z  green
1    B   Z  green
2    B   X    red
3    C   Y    red


# If you have multiple conditions use numpy.select()
# Example usage with np.select:
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')}) # Define df
print(df)
  Type Set
0    A   Z
1    B   Z
2    B   X
3    C   Y

# Set the conditions for determining values in new column:
conditions = [
    (df['Set'] == 'Z') & (df['Type'] == 'A'),
    (df['Set'] == 'Z') & (df['Type'] == 'B'),
    (df['Type'] == 'B')]

# Set the new column values in order of the conditions they should
#	correspond to:
choices = ['yellow', 'blue', 'purple']

# Add new column based on conditions and choices:
df['color'] = np.select(conditions, choices, default='black')

print(df)
# Returns:
  Set Type   color
0   Z    A  yellow
1   Z    B    blue
2   X    B  purple
3   Y    C   black

Example 5: add a value to an existing field in pandas dataframe after checking conditions

gapminder['gdpPercap_ind'] = gapminder.gdpPercap.apply(lambda x: 1 if x >= 1000 else 0)
gapminder.head()

Example 6: new column in pandas with where logic

virtsizes = {
  "type1": { "gb": 1.2, "xxx": 0, "yyy": 30 },
  "type2": { "gb": 1.5, "xxx": 2, "yyy": 20  },
  "type3": { "gb": 2.3, "xxx": 0.1, "yyy": 10  },
}
d = {k:v['gb'] for k,v in virtsizes.items()}
print (d)
{'type2': 1.5, 'type1': 1.2, 'type3': 2.3}

df = pd.DataFrame({'vol-type':['type1','type2']})
df["real_size"] = df["vol-type"].map(d)
print (df)
  vol-type  real_size
0    type1        1.2
1    type2        1.5