按id分组并对一列的值应用条件

pcrecxhr  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(260)

我是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
谢谢你的帮助!

gjmwrych

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

shyt4zoc

shyt4zoc2#

另一个版本,使用 .groupby() :

m = df.index.isin(
    df[df["LABEL"] == True].reset_index().groupby("ID").index.first()
)
df.loc[m, "NEW"] = df.loc[m, "STOCK"]
print(df)

印刷品:

ID        DATE  LABEL  STOCK    NEW
0  AA  01/26/2020   True    100  100.0
1  AA  01/27/2020  False    200    NaN
2  BB  01/28/2020  False    300    NaN
3  BB  01/29/2020   True    500  500.0
4  BB  01/30/2020  False    100    NaN
5  CC  01/26/2020   True    200  200.0
6  CC  01/27/2020  False    300    NaN
7  CC  01/28/2020  False    100    NaN
8  CC  01/29/2020   True    400    NaN

相关问题