« Pandas
Update data based on cond (condition) if cond=False then by NaN or by other
Parameters
cond : Condition to check , if False then value at other is replaced. If True then nothing is changed.
other : If cond is False then data given here is replaced.
inplace: Default is False , if it is set True then original DataFrame is changed.
axis : integer , default None , Alignment towards Axis ( if required )
level : Level of alignment if required.
error : default is 'raise' , It can take value 'raise' or 'ignore'
try_cast : default False,
Difference between MASK & WHERE
MASK: Data is updated as NaN (if
other is not given ) if
cond ( condition ) is True.
WHERE : Data is updated as NaN (if
other is not given ) if
cond ( condition ) is False.
DataFrame.mask()
Examples
Update where MATH column is more than 80
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 )
If we don't give cond value then it will be replaced with NaN.
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 * * * *
Multiple conditions with where
Replace with data more than 70 and less than 75.
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 * * * *
inplace
By default inplace=Flase , this will not change the original DataFrame. By making it to True that is inplace=True we can change the original DataFrame.
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
« loc « at « mask
« Pandas
Pandas DataFrame
iloc - rows and columns by integers »
← Subscribe to our YouTube Channel here