pandas change status in a column if condition code example
Example 1: pandas update with condition
import pandas as pd
import numpy as np
df = pd.DataFrame({'value':np.arange(1000000)})
df['value'] = np.where(df['value'] > 20000, 0, df['value'])
df.loc[df['value'] > 20000, 'value'] = 0
df['value'] = df['value'].mask(df['value'] > 20000, 0)
df['a'] = df.where(df.a <= 20000, 0)
Example 2: 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