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()
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 * * * *
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.