[英]Correct way to assign values to a dataframe column based on the values of other columns
[英]assign values to column based on two other columns in python
我有两列转推并提到这样
retweet mention
RT @CritCareMed:
@CellCellPress
RT @CritCareMed: @mother
RT @gvwilson:
RT @sciencemagazine:
RT @MHendr1cks: @nucAmbiguous
@air
我想根据是转推还是提及创建一个新列,如果是提及,则在新行中分配M,否则分配R。如果同时存在提及和转推,则该行的值应为M,R。 所以最终结果应该像
retweet mention Type
RT @CritCareMed: R
@CellCellPress M
RT @CritCareMed: @mother R,M
RT @gvwilson: R
RT @sciencemagazine: R
RT @MHendr1cks: @nucAmbiguous R,M
@air M
我现在在做什么
df = df.assign(Type=np.where(df.retweet.isnull(), 'M','R'))
但这给了我结果
retweet mention Type
RT @CritCareMed: NaN R
NaN @CellCellPress M
RT @CritCareMed: @mother M
RT @gvwilson: NaN R
RT @sciencemagazine: NaN R
RT @MHendr1cks: @nucAmbiguous M
NaN @air M
第3行和第6行的类型应该为R,M,但这只是给我M(如代码预期)。 如何修改代码以获得以上结果?
添加另一个条件以检查另一列:
df = df.assign(Type=np.where(df.retweet.isnull(), 'M',
np.where(df.mention.isnull(), 'R','R, M')))
print (df)
retweet mention Type
0 RT @CritCareMed: NaN R
1 NaN @CellCellPress M
2 RT @CritCareMed: @mother R, M
3 RT @gvwilson: NaN R
4 RT @sciencemagazine: NaN R
5 RT @MHendr1cks: @nucAmbiguous R, M
6 NaN @air M
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.