簡體   English   中英

Pandas 如果匹配字符串則刪除行(不區分大小寫)

[英]Pandas delete row if matches string (case-insensitive)

我正在嘗試刪除 dataframe 中的行,如果它包含我的列表中的某些內容。

東風

Search_List = ['drunk', 'owner']
df = df[~df['Searches'].str.contains('|'.join(Search_List))]

Searches
the person was Drunk
the owner was not available
This is a test
The Owner was not in

當前結果:

Searches
the person was Drunk
This is a test
The Owner was not in

預期結果:

Searches
This is a test

有沒有辦法進行搜索,但即使它是大寫等也允許它匹配? 我無法將其轉換為較低然后進行搜索,因為它破壞了我在腳本中的其他功能。 有一個簡單的解決方案嗎?

case=False參數添加到Series.str.contains

Search_List = ['drunk', 'owner']
df = df[~df['Searches'].str.contains('|'.join(Search_List), case=False)]
print (df)
         Searches
2  This is a test

您可以在檢查之前將字符串轉換為lowercase

df = df[~df['Searches'].str.lower().str.contains('|'.join(Search_List))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM