Search code examples
pythonpandasdataframewhere-clauseseries

Pandas: using where() method with another series?


I know what the where() method in pandas means. But in this case from the manual, I just cannot unterstand what they check with

t = pd.Series([True, False])

Can someone maybe help? Here is the code:

s = pd.Series(range(5))

t = pd.Series([True, False])

s.where(t, 99)

0     0
1    99
2    99
3    99
4    99

Solution

  • I will create dataframe to help you understand

    s.to_frame('s').assign(t=t, s_where1=s.where(t), s_where2=s.where(t, 99))
    

    result:

        s   t       s_where1    s_where2
    0   0   True    0.0         0            <-- if t is True, remain value of s
    1   1   False   NaN         99           <-- When not True, 
    2   2   NaN     NaN         99               where function  produce NaN in where1. 
    3   3   NaN     NaN         99               where function fills NaN with 99 in where2
    4   4   NaN     NaN         99               
    



    func where don't matter if size of the condition is the same as original series. think where func change except True