I have a polars dataframe, like:
import polars as pl
df = pl.DataFrame({"foo": [1.0, 2.0, 3.0], "bar": [11, 5, 8]})
How do I convert the first column to int64 type?
I was trying something like:
df.select(pl.col('foo')) = df.select(pl.col('foo')).cast(pl.Int64)
but it is not working.
In Pandas it was super easy:
df['foo'] = df['foo'].astype('int64')
Thanks.
Select your col
, cast
it to (int64
) and add it back to the original DataFrame with_columns
.
df = df.with_columns(pl.col("foo").cast(pl.Int64))
Output :
print(df)
shape: (3, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1 ┆ 11 │
│ 2 ┆ 5 │
│ 3 ┆ 8 │
└─────┴─────┘
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With