[英]Pandas multiple filter str.contains or not contains
我需要在表的2列結構上構建多個過濾器,該結構是7列,但是第一個“查詢”和最后一個“模板”正在過濾
我以前做過,但是行得通,但是現在(一年后)我不知道出了什么問題。
for item in glob.glob('D:\\path\\*.change'):
table = pd.read_csv(item, sep='\t', index_col=None)
#FILTERING
filtered_table = table[
(table['query'].str.contains("egg*", regex=True)==False) &
(table['query'].str.contains(".*phospho*", regex=True)==False) &
(table['query'].str.contains("vipe", regex=True)==False) &
(table['template'].str.contains("ABC1")) |
(table['template'].str.contains("bender")) ]
預期結果是該表中沒有包含字符串的行-egg *,。 磷酸 ,VIPE欄“查詢”並在列“模板”行包含“ABC1”或“彎”。
我認為您的情況中有些東西缺少括號。
嘗試這個:
table[(
# AND condition
table['query'].str.contains("egg*", regex=True)==False &
table['query'].str.contains(".*phospho*", regex=True)==False &
table['query'].str.contains("vipe", regex=True)==False &
# OR condition
(table['template'].str.contains("ABC1") |
table['template'].str.contains("bender"))
)]
我對問題的回答:
for item in glob.glob('D:\\path\\*.change'):
table = pd.read_csv(item, sep='\t', index_col=None)
#FILTERING
query_table = table[
(table['query'].str.contains("egg*", regex=True)==False) &
(table['query'].str.contains(".*phospho*", regex=True)==False) &
(table['query'].str.contains("vipe", regex=True)==False) ]
filtered_table = query_table[
(query_table['template'].str.contains("ABC1")) |
(query_table['template'].str.contains("bender")) ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.