1 | initial version |
To change the data types of columns within a DataFrame using Polars, you can use the cast
method.
Here's an example:
Assume you have a DataFrame df
with columns col1
and col2
of data types int64
and float64
, respectively, and you want to change col2
to a int64
data type.
import polars as pl
# Create a sample DataFrame
df = pl.DataFrame({
'col1': [1, 2, 3],
'col2': [1.0, 2.0, 3.0]
})
# Change col2 to int64 data type
df = df.lazy()
df = df.select([
pl.col('col1'),
pl.col('col2').cast(pl.Int64)
])
df = df.collect()
print(df)
Output:
shape: (3, 2)
┌───────┬───────┐
│ col1 ┆ col2 ┆
│ --- ┆ --- ┆
│ i64 ┆ i64 ┆
╞═══════╪═══════╡
│ 1 ┆ 1 ┆
│ 2 ┆ 2 ┆
│ 3 ┆ 3 ┆
└───────┴───────┘
Here, we first use the lazy
method to create a lazy DataFrame to avoid loading the data into memory. Then, we select the two columns, and use the cast
method to change the data type of col2
to int64
. Finally, we call the collect
method to execute the computation and get the result as a DataFrame.