Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

polars: add a constant to an existing column

$
0
0

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:

  1. why does df = df.with_columns(pl.col('random') += 500.0) throw a SyntaxError?

  2. various AIs tell me that df['random'] = df['random'] + 500 should also work, but it throws the following error instead:

    TypeError: DataFrame object does not support Series assignment by index

    Use 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.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>