1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to mask a polars dataframe using another dataframe?

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 19:33.

  1. Stack

    Stack Membro Participativo

    I have a polars dataframe like so:

    df = pl.from_repr("""
    ┌─────────────────────┬─────────┬─────────┐
    │ time ┆ 1 ┆ 2 │
    │ --- ┆ --- ┆ --- │
    │ datetime[μs] ┆ f64 ┆ f64 │
    ╞═════════════════════╪═════════╪═════════╡
    │ 2021-10-02 00:05:00 ┆ 2.9048 ┆ 2.8849 │
    │ 2021-10-02 00:10:00 ┆ 48224.0 ┆ 48068.0 │
    └─────────────────────┴─────────┴─────────┘
    """)


    and a masking dataframe with similar columns and time value like so:

    df_mask = pl.from_repr("""
    ┌─────────────────────┬───────┬───────┐
    │ time ┆ 1 ┆ 2 │
    │ --- ┆ --- ┆ --- │
    │ datetime[μs] ┆ bool ┆ bool │
    ╞═════════════════════╪═══════╪═══════╡
    │ 2021-10-02 00:05:00 ┆ false ┆ false │
    │ 2021-10-02 00:10:00 ┆ true ┆ true │
    └─────────────────────┴───────┴───────┘
    """)


    I am looking for this result:

    shape: (2, 3)
    ┌─────────────────────┬────────┬─────────┐
    │ time ┆ 1 ┆ 2 │
    │ --- ┆ --- ┆ --- │
    │ datetime[μs] ┆ f64 ┆ f64 │
    ╞═════════════════════╪════════╪═════════╡
    │ 2021-10-02 00:05:00 ┆ null ┆ null │
    │ 2021-10-02 00:10:00 ┆ 2.8849 ┆ 48068.0 │
    └─────────────────────┴────────┴─────────┘


    Here I only show with 2 columns '1' and '2' but there could any number of them.

    Any help would be appreciated!

    Continue reading...

Compartilhe esta Página