简体   繁体   English

根据不同长度的多个条件替换数据帧值

[英]Replace dataframe values based on multiple conditions of different length's

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? 可以说,对于name2,我想将第3块和第4块的“旋转”替换为“ VAC” ...对单衬板有任何建议吗?

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'

eq更改为isin

df.loc[df['Name'].eq('name2') & df['Block'].isin(['Block 3','Block 4']), 'Rotation'] = 'VAC'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM