簡體   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