[英]Filter pandas DataFrame by substring criteria
[英]Pandas DataFrame - Filter rows according to substring
假設我已經創建了一個數據框,並且我想過濾列 C 中的值不是列 B 中的值的子字符串的所有行。
例如 -
Column A Column B
string str
another arr
nope eee
如您所見,A 列中不存在 B 列中的任何值,但是字符串 'str' 是 'string' 的子字符串 - 我想過濾這些行並僅保留第 2 行和第 3 行。
謝謝大家!
嘗試:
mask = df[["Column A", "Column B"]].apply(lambda x: x["Column B"] in x["Column A"], axis=1)
res = df[mask]
print(res)
輸出
0 True
1 False
2 False
dtype: bool
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.