Question
Cast columns that might not exist in Polars
I want to cast a column to another type, but there is a possibility that the column does not exist in the df.
import polars as pl
import polars.selectors as cs
# Sample DataFrame
data = {
"viewCount": [100, 200, 300],
"likeCount": [10, 20, 30],
"id": [1, 2, 3]
}
df = pl.DataFrame(data)
# Columns we want to cast
columns_to_cast = ["viewCount", "likeCount", "favoriteCount", "id", "does_not_exist"]
# Check for existence and cast if the column exists
for col in columns_to_cast:
if col in df.columns:
df = df.with_columns(pl.col(col).cast(pl.Float64))
print(df)
Can the be achieved using polars' expression API?
2 37
2