【问题标题】:What is the equivalent of `DataFrame.drop_duplicates()` from pandas in polars?北极熊猫中的“DataFrame.drop_duplicates()”的等价物是什么?
【发布时间】:2022-08-15 17:49:47
【问题描述】:

北极熊猫中的drop_duplicates() 相当于什么?

import polars as pl
df = pl.DataFrame({\"a\":[1,1,2], \"b\":[2,2,3], \"c\":[1,2,3]})
df

输出:

shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ 2   ┆ 1   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 1   ┆ 2   ┆ 2   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 3   ┆ 3   │
└─────┴─────┴─────┘

代码:

df.drop_duplicates([\"a\", \"b\"])

提供以下错误:

AttributeError:未找到 drop_duplicates

    标签: python python-polars


    【解决方案1】:

    正确的函数名称是 .distinct()

    import polars as pl
    df = pl.DataFrame({"a":[1,1,2], "b":[2,2,3], "c":[1,2,3]})
    df.distinct(subset=["a","b"])
    

    这提供了正确的输出:

    shape: (2, 3)
    ┌─────┬─────┬─────┐
    │ a   ┆ b   ┆ c   │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ 1   ┆ 2   ┆ 1   │
    ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
    │ 2   ┆ 3   ┆ 3   │
    └─────┴─────┴─────┘
    

    【讨论】:

    • df.distinct() 可以不带任何参数运行。似乎它只是为了回答这个问题。 Polars 有非常好的文档字符串,运行 help(df.distinct)help(df.[method]) 来查找示例和默认参数。更多信息Polars Cookbook
    【解决方案2】:

    它重命名为 .unique()

    见他们的Python Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2014-06-12
      • 1970-01-01
      • 2022-11-28
      • 2014-05-08
      • 2012-07-20
      相关资源
      最近更新 更多