Polars DataFrame中df.to_markdown()的等价函数是什么?

3 投票
1 回答
88 浏览
提问于 2025-04-13 18:48

在Pandas中,你只需要简单地这样做

from IPython.display import display, Markdown

display(Markdown(my_df.to_markdown()))

就可以在笔记本中打印出漂亮的表格。

但是如果你有一个Polars的数据框,你会遇到这样的问题:

AttributeError: 'DataFrame'对象没有'to_markdown'这个属性

我现在的解决办法是

to_display = my_df.to_pandas()
display(Markdown(to_display.to_markdown()))

有没有更好的方法呢?

1 个回答

3

你可以使用 config 的 set_tbl_formatting 选项

比如说:

In [5]: with pl.Config() as cfg:
   ...:     cfg.set_tbl_formatting('ASCII_MARKDOWN')
   ...:     display(Markdown(repr(df)))
   ...:
shape: (2, 3)
| abc  | mno   | xyz   |
| ---  | ---   | ---   |
| f64  | str   | bool  |
|------|-------|-------|
| -2.5 | hello | true  |
| 5.0  | world | false |

想了解更多选项,可以查看 这个链接

撰写回答