用一维数组替换 Polars 列

问题描述 投票:0回答:1

样本df:

import polars as pl
import numpy as np
df = pl.DataFrame(
    {
        "nrs": [1, 2, 3, None, 5],
        "names": ["foo", "ham", "spam", "egg", None],
        "random": np.random.rand(5),
        "A": [True, True, False, False, False],
    }
)

我想更换列

random
。到目前为止,我一直在做

new = np.arange(5)
df.replace('random', pl.Series(new))

注意

replace
是少数可以就地工作的极坐标方法之一!

但现在我明白了

C:\Users\...\AppData\Local\Temp\ipykernel_18244\1406681700.py:2: DeprecationWarning: `replace` is deprecated. DataFrame.replace is deprecated and will be removed in a future version. Please use
    df = df.with_columns(new_column.alias(column_name))
instead.
  df = df.replace('random', pl.Series(new)) 

那么,我应该这样做吗

df = df.with_columns(pl.Series(new).alias('random'))

看起来更冗长,就地修改也消失了。我做的事情对吗?

replace python-polars
1个回答
0
投票

是的,你做得对。您需要按以下方式使用

with_columns

import polars as pl
import numpy as np

df = pl.DataFrame({
    "nrs": [1, 2, 3, None, 5],
    "names": ["foo", "ham", "spam", "egg", None],
    "random": np.random.rand(5), 
    "A": [True, True, False, False, False],
})

print(df)
new = np.arange(5)

new_series = pl.Series('random', new)

df_new = df.with_columns(new_series)

print(df_new)

这是原始的 df:

shape: (5, 4)
┌──────┬───────┬──────────┬───────┐
│ nrs  ┆ names ┆ random   ┆ A     │
│ ---  ┆ ---   ┆ ---      ┆ ---   │
│ i64  ┆ str   ┆ f64      ┆ bool  │
╞══════╪═══════╪══════════╪═══════╡
│ 1    ┆ foo   ┆ 0.736232 ┆ true  │
│ 2    ┆ ham   ┆ 0.017485 ┆ true  │
│ 3    ┆ spam  ┆ 0.940966 ┆ false │
│ null ┆ egg   ┆ 0.157872 ┆ false │
│ 5    ┆ null  ┆ 0.003914 ┆ false │
└──────┴───────┴──────────┴───────┘

这是新的

shape: (5, 4)
┌──────┬───────┬────────┬───────┐
│ nrs  ┆ names ┆ random ┆ A     │
│ ---  ┆ ---   ┆ ---    ┆ ---   │
│ i64  ┆ str   ┆ i64    ┆ bool  │
╞══════╪═══════╪════════╪═══════╡
│ 1    ┆ foo   ┆ 0      ┆ true  │
│ 2    ┆ ham   ┆ 1      ┆ true  │
│ 3    ┆ spam  ┆ 2      ┆ false │
│ null ┆ egg   ┆ 3      ┆ false │
│ 5    ┆ null  ┆ 4      ┆ false │
└──────┴───────┴────────┴───────┘
© www.soinside.com 2019 - 2024. All rights reserved.