Reputation: 135
I am able to replace dataframe values based on conditions of similar length, but I have not been able to successfully use a one-liner for conditions of different lengths. See below:
This approach works for conditions of similar length:
df = pd.DataFrame({'Name':['name1', 'name2', 'name3', 'name4', 'name2', 'name3', 'name4', 'name2', 'name2' ],
'Block':['Block 1','Block 1','Block 1', 'Block 1','Block 2','Block 2', 'Block 2','Block 3','Block 4'],
'Rotation':['ERJD','PEDI','MAM','PEDI', 'ERJD','PEDI','MAM','ERJD','ABD'],
})
df.loc[df['Name'].eq('name2') & df['Block'].eq('Block 3'), 'Rotation'] = 'VAC'
which generates
Name Block Rotation
0 name1 Block 1 ERJD
1 name2 Block 1 PEDI
2 name3 Block 1 MAM
3 name4 Block 1 PEDI
4 name2 Block 2 ERJD
5 name3 Block 2 PEDI
6 name4 Block 2 MAM
7 name2 Block 3 VAC
8 name2 Block 4 ABD
Lets say for name2 i want to replace the Rotation to 'VAC' for both Block 3 and Block 4...Any suggestions for a one-liner?
I tried using a similar approach below, but cant get it to work.
df.loc[df['Name'].eq('name2') & df['Block'].eq(['Block 3','Block 4']), 'Rotation'] = 'VAC'
Upvotes: 0
Views: 51
Reputation: 323226
Change the eq
to isin
df.loc[df['Name'].eq('name2') & df['Block'].isin(['Block 3','Block 4']), 'Rotation'] = 'VAC'
Upvotes: 2