Py Polars:如何像 SQL 中那样使用 'in' 和 'not in' 进行过滤
Py Polars: How to filter using 'in' and 'not in' like in SQL
如何实现 SQL 的 IN 和 NOT IN 的等价物?
我有一个包含所需值的列表。场景如下:
import pandas as pd
import polars as pl
exclude_fruit = ["apple", "orange"]
df = pl.DataFrame(
{
"A": [1, 2, 3, 4, 5, 6],
"fruits": ["banana", "banana", "apple", "apple", "banana", "orange"],
"B": [5, 4, 3, 2, 1, 6],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle", "frog"],
"optional": [28, 300, None, 2, -30, 949],
}
)
df.filter(~pl.select("fruits").str.contains(exclude_fruit))
df.filter(~pl.select("fruits").to_pandas().isin(exclude_fruit))
df.filter(~pl.select("fruits").isin(exclude_fruit))
你很接近。
df.filter(~pl.col('fruits').is_in(exclude_fruit))
shape: (3, 5)
┌─────┬────────┬─────┬────────┬──────────┐
│ A ┆ fruits ┆ B ┆ cars ┆ optional │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str ┆ i64 │
╞═════╪════════╪═════╪════════╪══════════╡
│ 1 ┆ banana ┆ 5 ┆ beetle ┆ 28 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 2 ┆ banana ┆ 4 ┆ audi ┆ 300 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 5 ┆ banana ┆ 1 ┆ beetle ┆ -30 │
└─────┴────────┴─────┴────────┴──────────┘
如何实现 SQL 的 IN 和 NOT IN 的等价物?
我有一个包含所需值的列表。场景如下:
import pandas as pd
import polars as pl
exclude_fruit = ["apple", "orange"]
df = pl.DataFrame(
{
"A": [1, 2, 3, 4, 5, 6],
"fruits": ["banana", "banana", "apple", "apple", "banana", "orange"],
"B": [5, 4, 3, 2, 1, 6],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle", "frog"],
"optional": [28, 300, None, 2, -30, 949],
}
)
df.filter(~pl.select("fruits").str.contains(exclude_fruit))
df.filter(~pl.select("fruits").to_pandas().isin(exclude_fruit))
df.filter(~pl.select("fruits").isin(exclude_fruit))
你很接近。
df.filter(~pl.col('fruits').is_in(exclude_fruit))
shape: (3, 5)
┌─────┬────────┬─────┬────────┬──────────┐
│ A ┆ fruits ┆ B ┆ cars ┆ optional │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str ┆ i64 │
╞═════╪════════╪═════╪════════╪══════════╡
│ 1 ┆ banana ┆ 5 ┆ beetle ┆ 28 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 2 ┆ banana ┆ 4 ┆ audi ┆ 300 │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 5 ┆ banana ┆ 1 ┆ beetle ┆ -30 │
└─────┴────────┴─────┴────────┴──────────┘