pandas new column if statement code example
Example 1: python conditionally create new column in pandas dataframe
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
print(df)
Type Set
0 A Z
1 B Z
2 B X
3 C Y
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
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
print(df)
Type Set
0 A Z
1 B Z
2 B X
3 C Y
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)
Set Type color
0 Z A yellow
1 Z B blue
2 X B purple
3 Y C black
Example 2: 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