繁体   English   中英

如何根据 pandas 中的条件删除一行?

[英]How to remove a row based on a condition in pandas?

我有以下 dataframe:

指数 描述
0 用户 A 打开的类型为 yyy 的选项卡 tab_1
1个 一些值
2个 用户 B 打开的 xxx 类型的选项卡 tab_1
3个 用户 A 打开的类型为 yyy 的选项卡 tab_4
4个 一些值
5个 类型为 yyy 的选项卡 tab_1 已被用户 A 关闭
6个 一些值
7 用户 B 关闭了 xxx 类型的选项卡 tab_1
8个 类型为 yyy 的选项卡 tab_2 已被用户 A 关闭
9 一些值
10 类型为 zzz 的选项卡 tab_3 已被用户 C 关闭

我想删除“描述”列中的单元格没有成对的行。 我所说的成对是指第 0 行和第 5 行,以及第 2 行和第 7 行。第 3、8 和 10 行没有它们的对 - 某个选项卡由某个用户打开但未关闭或已关闭但未打开。

预计 output:

指数 描述
0 用户 A 打开的类型为 yyy 的选项卡 tab_1
1个 一些值
2个 用户 B 打开的 xxx 类型的选项卡 tab_1
4个 一些值
5个 类型为 yyy 的选项卡 tab_1 已被用户 A 关闭
6个 一些值
7 用户 B 关闭了 xxx 类型的选项卡 tab_1
9 一些值

有没有办法做到这一点?

你可以试试这个 function duplicatedhttps://pandas.pydata.org/docs/reference/api/pandas.DataFrame.duplicated.html

例如:

df_new = df.duplicated(subset=['Description'])

老实说,我不确定这是不是你需要的,但无论如何你可以试试这个:

mask = (df.groupby(df['Description'].str.replace('opened|closed','',regex=True))['Description'].
        transform(lambda x: (x.str.contains('opened').any())&(x.str.contains('closed').any())))

res = df.loc[mask]

>>> res
'''
                                  
Index                             Description           
0      Tab tab_1 of type yyy opened by User A
2      Tab tab_1 of type xxx opened by User B
5      Tab tab_1 of type yyy closed by User A
7      Tab tab_1 of type xxx closed by User B

用 null 替换打开和关闭的文本,然后将过滤(dataframegroupby 方法)应用于 select,其中出现次数为 1,然后将其删除

data.drop(data.groupby(data['Description'].str.replace('opened|closed','',regex=True)).filter(lambda x: x['Description'].count() == 1).index)
Index   Description
    0   Tab tab_1 of type yyy opened by User A
    1   some_value
    2   Tab tab_1 of type xxx opened by User B
    4   some_value
    5   Tab tab_1 of type yyy closed by User A
    6   some_value
    7   Tab tab_1 of type xxx closed by User B
    9   some_value

pandas DataFrames have method duplicated ,这正是你需要的: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.duplicated.html

df.drop_duplicates('Description')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM