gpt4 book ai didi

python - 根据一个特定值 Pandas 删除重复项

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:05 25 4
gpt4 key购买 nike

我有一个数据框,我试图在其中基于子集删除重复项,但仅针对特定值。

我有以下数据框:

Date          Name        Task  Hours  

2019-09-26 John Smith A 24
2019-09-26 Bruce Pitt A 24
2019-09-27 John Smith A 12
2019-09-27 John Smith B 12
2019-09-28 Emma Garcia A 24
2019-09-28 Emma Garcia E 24

我想根据 DateNameHours 删除重复的行,但仅限于 hours = 24

我知道如何删除重复项,但我不知道如何在此行中添加此特定条件值:

df1.drop_duplicates(subset=['Date', 'Name','Hours'],keep='first', inplace=True)

预期输出:

Date          Name        Task  Hours  

2019-09-26 John Smith A 24
2019-09-26 Bruce Pitt A 24
2019-09-27 John Smith A 12
2019-09-27 John Smith B 12
2019-09-28 Emma Garcia A 24

最佳答案

这是重复的

df[~(df.duplicated(['Date','Name','Hours']) & df.Hours.eq(24))]
Out[53]:
Date Name Task Hours
0 2019-09-26 JohnSmith A 24
1 2019-09-26 BrucePitt A 24
2 2019-09-27 JohnSmith A 12
3 2019-09-27 JohnSmith B 12
4 2019-09-28 EmmaGarcia A 24

关于python - 根据一个特定值 Pandas 删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58119531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com