Pandas DataFrame where

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,

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.where(my_data['MATH'] > 80,-9) 
print(my_data)
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.
my_data=my_data.where(my_data['MATH'] > 80) 
print(my_data)
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
my_data=my_data.where(my_data['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]}
my_data = pd.DataFrame(data=my_dict)
my_cond= (my_data['MATH'] >70) &  (my_data['MATH'] <75) 
my_data=my_data.where(my_cond,'*') 
print(my_data)
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]}
my_data = pd.DataFrame(data=my_dict)
my_cond= (my_data['MATH'] >70) &  (my_data['MATH'] <75) 
my_data.where(my_cond,inplace=False) 
print(my_data)
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
loc at mask

Pandas Pandas DataFrame iloc - rows and columns by integers


plus2net.com



Post your comments , suggestion , error , requirements etc here




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