You can use DataFrame.with_row_index():
import polars as pl
df = pl.DataFrame({"test": np.arange(1, 11)})
print(
df.with_row_index()
.with_columns(
pl.when(pl.col("index") < 5)...
Not sure which output exactly you're expecting, but here's an example of incrementing the counter only at rows which meet the criteria, using cum_sum():
df.with_columns(
pl.when(pl.col('code') ==...
The reason that your sample code doesn't work as expected is that Polars write_excel() applies a cell format for numbers and that overwrites the row format.
You can control column or dtype formatting...
You can keep the Event objects by passing return_dtype=pl.Object
df.select(pl.col("events").map_elements(event_table))
shape: (5, 1)
┌───────────────────────────────────┐
│ events...
While the solution using the walrus operator works. It is probably more idiomatic and cleaner to use an pl.when().then() construct in conjunction with pl.int_range() to create the event_id.
(
df...
I think you found out a very unique/interesting and clever solution. Consider also just iterating over columns:
df.select(column / scalars[column.name] for column in df.iter_columns())
or
df.select(p...
You could install duckdb-engine, and write the database using the connection URI string.
df.write_database(
table_name='test_table',
connection="duckdb:///scratch.db",
)
Reading the data back...
You can compute the max per group over df1, then clip df2:
out = df2.with_columns(
pl.col('index').clip(
upper_bound=df1.select(pl.col('index').max().over('group'))['index']
)
)
Outp...
Lost your password? Please enter your email address. You will receive a link to create a new password.
Donate
Thank you for visit here to donate to us, you think it's just a cup of coffee, but with us, it's really meaningful and contributes to help this blog grow more and more.
You can support us in one of the ways below: