我是Pandas图书馆的新手,非常感谢您的帮助。这是我的意见
IDDateLabelstockaaa01/26/2020true100aa01/27/2020false200bb01/28/2020false300bb01/29/2020true500bb01/30/2020false100cc01/26/2020true200cc01/27/2020false300cc01/28/2020false100cc01/29/2020true400
我想看看label列,对于为每个id找到的第一个“true”标签,我需要将stock的值复制到一个新列中。如果一个id有多个true,我只需要考虑第一个。
下面是我想要获得的输出:
iddatelabelstocknewaa01/26/2020true100100aa01/27/2020false200bb01/28/2020false300bb01/29/2020true500bb01/30/2020false100cc01/26/2020true200cc01/27/2020false300cc01/28/2020false100cc01/29/2020true400
谢谢你的帮助!
2条答案
按热度按时间gjmwrych1#
将双布尔值与
.duplicated()
```df.loc[df['LABEL'].eq(True) &
df.duplicated(subset=['ID'],keep='last'),'NEW'] = df['STOCK']
print(df.fillna(''))
ID DATE LABEL STOCK NEW
0 AA 01/26/2020 True 100 100
1 AA 01/27/2020 False 200
2 BB 01/28/2020 False 300
3 BB 01/29/2020 True 500 500
4 BB 01/30/2020 False 100
5 CC 01/26/2020 True 200 200
6 CC 01/27/2020 False 300
7 CC 01/28/2020 False 100
8 CC 01/29/2020 True 400
df.assign(ky1=df['LABEL'].eq(True),
ky2=df.duplicated(subset=['ID'],keep='last')
)
ID DATE LABEL STOCK NEW ky1 ky2
0 AA 01/26/2020 True 100 100.0 True True
1 AA 01/27/2020 False 200 NaN False False
2 BB 01/28/2020 False 300 NaN False True
3 BB 01/29/2020 True 500 500.0 True True
4 BB 01/30/2020 False 100 NaN False False
5 CC 01/26/2020 True 200 200.0 True True
6 CC 01/27/2020 False 300 NaN False True
7 CC 01/28/2020 False 100 NaN False True
8 CC 01/29/2020 True 400 NaN True False
shyt4zoc2#
另一个版本,使用
.groupby()
:印刷品: