在此示例中,在 columns 上["foo", "ham"],我希望删除第 1 行和第 4 行,因为它们与列表中的一对匹配
df = pl.DataFrame(
{
"foo": [1, 1, 2, 2, 3, 3, 4],
"bar": [6, 7, 8, 9, 10, 11, 12],
"ham": ["a", "b", "c", "d", "e", "f", "b"]
}
)
pairs = [(1,"b"),(3,"e"),(4,"g")]
Run Code Online (Sandbox Code Playgroud)
以下内容对我有用,但我认为当数据框和对列表很大时这会出现问题。
for a, b in pairs:
df = df.filter(~(pl.col('foo') == a) | ~(pl.col('ham') == b))
Run Code Online (Sandbox Code Playgroud)
我认为这是这个问题的 Pandas 实现Pandas: How to remove rows from a dataframe based on a tuples list made value in TWO columns?
我不确定 …