如何将 Polars 数据帧列类型从 float64 转换为 int64?

lmo*_*csi 2 python dataframe python-polars

我有一个极坐标数据框,例如:

import polars as pl
df = pl.DataFrame({"foo": [1.0, 2.0, 3.0], "bar": [11, 5, 8]})
Run Code Online (Sandbox Code Playgroud)

如何将第一列转换为 int64 类型?
我正在尝试类似的事情:

df.select(pl.col('foo')) = df.select(pl.col('foo')).cast(pl.Int64)
Run Code Online (Sandbox Code Playgroud)

但它不起作用。
在 Pandas 中这非常简单:

df['foo'] = df['foo'].astype('int64')
Run Code Online (Sandbox Code Playgroud)

谢谢。

Tim*_*ess 5

选择您的col( cast)int64并将其添加回原始DataFrame with_columns

\n
df = df.with_columns(pl.col("foo").cast(pl.Int64))\n
Run Code Online (Sandbox Code Playgroud)\n

输出 :

\n
print(df)\n\nshape: (3, 2)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 foo \xe2\x94\x86 bar \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1   \xe2\x94\x86 11  \xe2\x94\x82\n\xe2\x94\x82 2   \xe2\x94\x86 5   \xe2\x94\x82\n\xe2\x94\x82 3   \xe2\x94\x86 8   \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n