【问题标题】:Drop pandas rows if a value repeat more than X times [duplicate]如果值重复超过 X 次,则删除 pandas 行 [重复]
【发布时间】:2020-07-28 19:56:02
【问题描述】:

如何删除列的值重复超过 2 次的行。它可以是连续的,也可以不是连续的。喜欢:

NAME      EMAIL
Joe       joe@email.com
John      joe@email.com
Eric      eric@mymail.com
Melissa   mel@email.com
Ron       joe@email.com

我想删除所有带有 joe@email.com 的行,因为它重复了 2 次以上。

【问题讨论】:

标签: python pandas


【解决方案1】:

创建你的数据框

import pandas as pd
import numpy as np

data = {'Name': ['Michael', 'Larry', 'Shaq', 'barry'], 'email': ['asf@gmail.com', 'akfd@gmail.com', 'asf@gmail.com', 'asf@gmail.com'] }

df1 = pd.DataFrame.from_dict(data)

print(df1)

      Name           email
0  Michael   asf@gmail.com
1    Larry  akfd@gmail.com
2     Shaq   asf@gmail.com
3    barry   asf@gmail.com

然后按列中大于 2 的值对其进行过滤

fil =  df1.groupby('email').filter(lambda x : len(x)<2)

print(fil)

    Name           email
1  Larry  akfd@gmail.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2021-10-10
    • 2010-11-13
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2018-08-11
    相关资源
    最近更新 更多