Yogesh
Yogesh

Reputation: 83

Updating Value in Series Based on Another Series with condition

I have two series sr1 and sr2:

sr1 = pd.Series([1, 2, 3, 4, 5])
sr2 = pd.Series([6, 0, 0, 4 ,9])

I want to update series 1 i.e sr1 from sr2. If sr2 is 0 then same value should be updated in sr1 for same index. I want below output :

       sr1

    0   1
    1   0
    2   0
    3   4
    4   5

Upvotes: 1

Views: 101

Answers (2)

Mohammad Nazari
Mohammad Nazari

Reputation: 3025

if you use series, You can use the following code:

sr1 = pd.Series([1, 2, 3, 4, 5]) 
sr2 = pd.Series([6, 0, 0, 4 ,9])
sr1=sr1.mask(sr2==0,0)

Upvotes: 0

BENY
BENY

Reputation: 323276

Try with mask

df1=df1.mask(df2.A==0,0)
   A
0  1
1  0
2  0
3  4
4  5

Upvotes: 2

Related Questions