Dataframe:
rng = np.random.default_rng(42)df = pl.DataFrame( {"nrs": [1, 2, 3, None, 5],"names": ["foo", "ham", "spam", "egg", None],"random": rng.random(5),"A": [True, True, False, False, False], })Currently, to add a constant to a column I do:
df = df.with_columns(pl.col('random') + 500.0)Questions:
why does
df = df.with_columns(pl.col('random') += 500.0)throw aSyntaxError?various AIs tell me that
df['random'] = df['random'] + 500should also work, but it throws the following error instead:TypeError: DataFrame object does not support
Seriesassignment by indexUse
DataFrame.with_columns.
This just goes to show how trustworthy AIs are, today! But why is polars throwing an error? I've been using df['random'] to identify the random column in other parts of my code, and it worked.