The easiest way to accomplish this is with the cast expression.
String to Int/Float
To cast from a string to an integer (or float):
import polars as pl
df = pl.DataFrame({"bar": ["100", "250", "125", ""]})
df.with_columns(pl.col('bar').cast(pl.Int64, strict=False).alias('bar_int'))
shape: (4, 2)
βββββββ¬ββββββββββ
β bar β bar_int β
β --- β --- β
β str β i64 β
βββββββͺββββββββββ‘
β 100 β 100 β
β 250 β 250 β
β 125 β 125 β
β β null β
βββββββ΄ββββββββββ
A handy list of available datatypes is here. These are all aliased under polars, so you can refer to them easily (e.g., pl.UInt64).
For the data you describe, I recommend using strict=False to avoid having one mangled number among millions of records result in an exception that halts everything.
Int/Float to String
The same process can be used to convert numbers to strings - in this case, the utf8 datatype.
Let me modify your dataset slightly:
df = pl.DataFrame({"bar": [100.5, 250.25, 1250000, None]})
df.with_columns(pl.col("bar").cast(pl.String, strict=False).alias("bar_string"))
shape: (4, 2)
ββββββββββ¬βββββββββββββ
β bar β bar_string β
β --- β --- β
β f64 β str β
ββββββββββͺβββββββββββββ‘
β 100.5 β 100.5 β
β 250.25 β 250.25 β
β 1.25e6 β 1250000.0 β
β null β null β
ββββββββββ΄βββββββββββββ
If you need more control over the formatting, you can use the map_elements method and Python's new f-string formatting.
df.with_columns(
pl.col("bar").map_elements(lambda x: f"This is ${x:,.2f}!").alias("bar_fstring")
)
shape: (4, 2)
ββββββββββ¬βββββββββββββββββββββββββ
β bar β bar_fstring β
β --- β --- β
β f64 β str β
ββββββββββͺβββββββββββββββββββββββββ‘
β 100.5 β This is $100.50! β
β 250.25 β This is $250.25! β
β 1.25e6 β This is $1,250,000.00! β
β null β null β
ββββββββββ΄βββββββββββββββββββββββββ
I found this web page to be a handy reference for those unfamiliar with f-string formatting.
Answer from user18559875 on Stack OverflowThe easiest way to accomplish this is with the cast expression.
String to Int/Float
To cast from a string to an integer (or float):
import polars as pl
df = pl.DataFrame({"bar": ["100", "250", "125", ""]})
df.with_columns(pl.col('bar').cast(pl.Int64, strict=False).alias('bar_int'))
shape: (4, 2)
βββββββ¬ββββββββββ
β bar β bar_int β
β --- β --- β
β str β i64 β
βββββββͺββββββββββ‘
β 100 β 100 β
β 250 β 250 β
β 125 β 125 β
β β null β
βββββββ΄ββββββββββ
A handy list of available datatypes is here. These are all aliased under polars, so you can refer to them easily (e.g., pl.UInt64).
For the data you describe, I recommend using strict=False to avoid having one mangled number among millions of records result in an exception that halts everything.
Int/Float to String
The same process can be used to convert numbers to strings - in this case, the utf8 datatype.
Let me modify your dataset slightly:
df = pl.DataFrame({"bar": [100.5, 250.25, 1250000, None]})
df.with_columns(pl.col("bar").cast(pl.String, strict=False).alias("bar_string"))
shape: (4, 2)
ββββββββββ¬βββββββββββββ
β bar β bar_string β
β --- β --- β
β f64 β str β
ββββββββββͺβββββββββββββ‘
β 100.5 β 100.5 β
β 250.25 β 250.25 β
β 1.25e6 β 1250000.0 β
β null β null β
ββββββββββ΄βββββββββββββ
If you need more control over the formatting, you can use the map_elements method and Python's new f-string formatting.
df.with_columns(
pl.col("bar").map_elements(lambda x: f"This is ${x:,.2f}!").alias("bar_fstring")
)
shape: (4, 2)
ββββββββββ¬βββββββββββββββββββββββββ
β bar β bar_fstring β
β --- β --- β
β f64 β str β
ββββββββββͺβββββββββββββββββββββββββ‘
β 100.5 β This is $100.50! β
β 250.25 β This is $250.25! β
β 1.25e6 β This is $1,250,000.00! β
β null β null β
ββββββββββ΄βββββββββββββββββββββββββ
I found this web page to be a handy reference for those unfamiliar with f-string formatting.
As an addition to @cbilot 's answer.
You don't need to use slow python lambda functions to use special string formatting of expressions. Polars has a format function for this purpose:
df = pl.DataFrame({"bar": ["100", "250", "125", ""]})
df.with_columns(
pl.format("This is {}!", pl.col("bar"))
)
shape: (4, 2)
βββββββ¬βββββββββββββββ
β bar β literal β
β --- β --- β
β str β str β
βββββββͺβββββββββββββββ‘
β 100 β This is 100! β
β 250 β This is 250! β
β 125 β This is 125! β
β β This is ! β
βββββββ΄βββββββββββββββ