import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,70,70,82,30],
'ENGLISH':[81,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict)
df=df.where(df['MATH'] > 80,-9)
print(df)
Output
NAME ID MATH ENGLISH
0 -9 -9 -9 -9
1 -9 -9 -9 -9
2 -9 -9 -9 -9
3 -9 -9 -9 -9
4 King 5 82 60
5 -9 -9 -9 -9
You can check that the all data of 4th row is not replaced as math value at 4th row is 82 ( True ) df=df.where(df['MATH'] > 80)
print(df)
Output
NAME ID MATH ENGLISH
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 NaN NaN NaN NaN
4 King 5.0 82.0 60.0
5 NaN NaN NaN NaN
Replacing with string
df=df.where(df['MATH']>80,'*')
Output
NAME ID MATH ENGLISH
0 * * * *
1 * * * *
2 * * * *
3 * * * *
4 King 5 82 60
5 * * * *
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,72,70,82,30],
'ENGLISH':[81,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict)
my_cond= (df['MATH'] >70) & (df['MATH'] <75)
df=df.where(my_cond,'*')
print(df)
Output
NAME ID MATH ENGLISH
0 * * * *
1 * * * *
2 Alex 3 72 40
3 * * * *
4 * * * *
5 * * * *
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,72,70,82,30],
'ENGLISH':[81,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict)
my_cond= (df['MATH'] >70) & (df['MATH'] <75)
df.where(my_cond,inplace=False)
print(df)
Output: Now the original DataFrame will not change.
NAME ID MATH ENGLISH
0 Ravi 1 80 81
1 Raju 2 40 70
2 Alex 3 72 40
3 Ron 4 70 50
4 King 5 82 60
5 Jack 6 30 30
Replace data based multiple condition like CASE THEN ( SQL ) by using np.where
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.