如何在不包括前导的 ‘shape’ 信息的情况下打印 Polars 数据框。

huangapple go评论74阅读模式
英文:

How to print Polars dataframe without the leading 'shape' information

问题

I will provide the translation without the code part. Here is the translated text:

当我打印 Polars 数据框(例如,从终端或在 Jupyter 笔记本中),会出现一个带有结果数据框形状的字符串。我正在寻找不打印此作为结果的方法。

简单示例如下:

>>> import polars as pl
>>> df = pl.DataFrame()
>>> df
shape: (0, 0)
┌┐
╞╡
└┘
>>> print(df)
shape: (0, 0)
┌┐
╞╡
└┘
>>> print(df[0, :])
shape: (0, 0)
┌┐
╞╡
└┘

我的问题是如何仅打印数据框,而不带着那个隐藏的 "shape: [n, m]" 字符串?

英文:

When I print a Polars dataframe (e.g., from terminal or within Jupyter notebook), there is a leading string citing the shape of the resultant dataframe. I am looking for the method to not have this printed as part of the result.

The simple example would be the following:

>>> import polars as pl
>>> df = pl.DataFrame()
>>> df
shape: (0, 0)
┌┐
╞╡
└┘
>>> print(df)
shape: (0, 0)
┌┐
╞╡
└┘
>>> print(df[0, :])
shape: (0, 0)
┌┐
╞╡
└┘

My question is how would I print only the dataframe without the lurking "shape: [n, m]" string?

答案1

得分: 3

I found a native way: you could use pl.Config.set_tbl_hide_dataframe_shape. This

import polars as pl

pl.Config.set_tbl_hide_dataframe_shape(True)

df = pl.DataFrame({'col1': range(3), 'col2': ['a', 'b', 'c']})
print(df)

results in

┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ i64  ┆ str  │
╞══════╪══════╡
│ 0    ┆ a    │
│ 1    ┆ b    │
│ 2    ┆ c    │
└──────┴──────┘
英文:

I found a native way: you could use pl.Config.set_tbl_hide_dataframe_shape. This

import polars as pl

pl.Config.set_tbl_hide_dataframe_shape(True)

df = pl.DataFrame({'col1': range(3), 'col2': ['a', 'b', 'c']})
print(df)

results in

┌──────┬──────┐
│ col1 ┆ col2 │
│ ---  ┆ ---  │
│ i64  ┆ str  │
╞══════╪══════╡
│ 0    ┆ a    │
│ 1    ┆ b    │
│ 2    ┆ c    │
└──────┴──────┘

huangapple
  • 本文由 发表于 2023年3月15日 21:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定