Pandas DataFrame mask


Youtube Live session on Tkinter

Pandas

Update data based on cond (condition) if cond=True then by NaN or by other
Parameters

cond : Condition to check , if True then value at other is replaced. If False then nothing is changed.
other : If cond is True 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,

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]}
my_data = pd.DataFrame(data=my_dict)
my_data=my_data.mask(my_data['MATH'] > 80,-5) 
print(my_data)
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       81
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4    -5  -5    -5       -5
5  Jack   6    30       30
You can check that the all data of 4th row is replaced by -5.
We want to replace only cell data not the full row data.
my_data['MATH']=my_data['MATH'].mask(my_data['MATH'] > 80,-5) 
print(my_data)
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       81
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    -5       60
5  Jack   6    30       30

Multiple conditions with MASK

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,73,70,82,30],
         'ENGLISH':[81,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_cond= (my_data['MATH'] >70) &  (my_data['MATH'] <75) 
replace=-7
my_data['MATH'].mask(my_cond,replace,inplace=True) 
print(my_data)
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       81
1  Raju   2    40       70
2  Alex   3    -7       40
3   Ron   4    70       50
4  King   5    82       60
5  Jack   6    30       30

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.
my_data['MATH'].mask(my_data['MATH'] > 80,-5,inplace=True)
Output: Now the original DataFrame will change.
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       81
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    -5       60
5  Jack   6    30       30
Replace data based multiple condition like CASE THEN ( SQL ) by using np.where
loc at where

Pandas Pandas DataFrame iloc - rows and columns by integers
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer