Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 mask don’t matter if size of the condition is the same as original series. think where func change except True

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading