如何在Polars中按数据类型选择列?

ast*_*nlu 6 dataframe python-polars

在 pandas 中,我们有pandas.DataFrame.select_dtypes根据 .pandas 文件选择某些列的方法dtype。在 Polars 中是否有类似的方法来做这样的事情?

ast*_*nlu 10

可以将数据类型传递给pl.col

\n
import polars as pl\n\ndf = pl.DataFrame(\n    {\n        "id": [1, 2, 3],\n        "name": ["John", "Jane", "Jake"],\n        "else": [10.0, 20.0, 30.0],\n    }\n)\nprint(df.select([pl.col(pl.Utf8), pl.col(pl.Int64)]))\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
shape: (3, 2)\n\xe2\x94\x8c\xe2\x94\x80\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 name \xe2\x94\x86 id  \xe2\x94\x82\n\xe2\x94\x82 ---  \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str  \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\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 John \xe2\x94\x86 1   \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 Jane \xe2\x94\x86 2   \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 Jake \xe2\x94\x86 3   \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\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


Abd*_*P M 6

Polars 0.18.1开始,您可以使用polars.selectors.by_dtype选择器选择与给定 dtypes 匹配的所有列

\n
>>> import polars as pl\n>>> import polars.selectors as cs\n>>> \n>>> df = pl.DataFrame(\n...     {\n...         "id": [1, 2, 3],\n...         "name": ["John", "Jane", "Jake"],\n...         "else": [10.0, 20.0, 30.0],\n...     }\n... )\n>>> \n>>> print(df.select(cs.by_dtype(pl.Utf8, pl.Int64)))\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\x80\xe2\x94\x90\n\xe2\x94\x82 id  \xe2\x94\x86 name \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 ---  \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 str  \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\x90\xe2\x95\xa1\n\xe2\x94\x82 1   \xe2\x94\x86 John \xe2\x94\x82\n\xe2\x94\x82 2   \xe2\x94\x86 Jane \xe2\x94\x82\n\xe2\x94\x82 3   \xe2\x94\x86 Jake \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\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n

要选择所有非数字类型列:

\n
>>> import polars as pl\n>>> import polars.selectors as cs\n>>> \n>>> df = pl.DataFrame(\n...     {\n...         "id": [1, 2, 3],\n...         "name": ["John", "Jane", "Jake"],\n...         "else": [10.0, 20.0, 30.0],\n...     }\n... )\n>>> \n>>> print(df.select(~cs.by_dtype(pl.NUMERIC_DTYPES)))\n>>> # OR print(df.select(~cs.numeric()))\nshape: (3, 1)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x82\n\xe2\x94\x82 ---  \xe2\x94\x82\n\xe2\x94\x82 str  \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 John \xe2\x94\x82\n\xe2\x94\x82 Jane \xe2\x94\x82\n\xe2\x94\x82 Jake \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\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