我有两个专栏转发,并提到如下
retweet mention
RT @CritCareMed:
@CellCellPress
RT @CritCareMed: @mother
RT @gvwilson:
RT @sciencemagazine:
RT @MHendr1cks: @nucAmbiguous
@air我想要一个新的列,基于它是一个转发,还是在新的行中提到和赋值M,如果它是一个提到,其他赋值R。如果提到和转发都存在,那么行应该有值M,因此最终结果应该是
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(正如代码中所期望的)。如何修改代码以获得上述结果?
发布于 2017-10-18 11:24:49
添加检查另一列的另一个条件:
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 Mhttps://stackoverflow.com/questions/46808866
复制相似问题