I have a polars DataFrame with multiple numeric (float dtype) columns. I want to write some of them to a csv with a certain number of decimal places. The number of decimal places I want is column-specific.
polars
offers format:
import polars as pl
df = pl.DataFrame({"a": [1/3, 1/4, 1/7]})
df.select(
[
pl.format("as string {}", pl.col("a")),
]
)
shape: (3, 1)
┌───────────────────────────────┐
│ literal │
│ --- │
│ str │
╞═══════════════════════════════╡
│ as string 0.3333333333333333 │
│ as string 0.25 │
│ as string 0.14285714285714285 │
└───────────────────────────────┘
However, if I try to set a directive to specify number of decimal places, it fails:
df.select(
[
pl.format("{:.3f}", pl.col("a")),
]
)
ValueError: number of placeholders should equal the number of arguments
Is there an option to have "real" f-string functionality without using an apply
?
pl.__version__: '0.16.16'
- related: Polars: switching between dtypes within a DataFrame
- to set the decimal places of all output columns, pl.DataFrame.write_csv offers the
float_precision
keyword