Python 漂亮地打印整个 Pandas 系列 / DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19124601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pretty-print an entire Pandas Series / DataFrame
提问by Dun Peal
I work with Series and DataFrames on the terminal a lot. The default __repr__
for a Series returns a reduced sample, with some head and tail values, but the rest missing.
我经常在终端上使用 Series 和 DataFrames。__repr__
系列的默认值返回一个减少的样本,具有一些头部和尾部值,但其余的缺失。
Is there a builtin way to pretty-print the entire Series / DataFrame? Ideally, it would support proper alignment, perhaps borders between columns, and maybe even color-coding for the different columns.
是否有内置的方法来漂亮地打印整个系列/数据帧?理想情况下,它会支持正确对齐,可能是列之间的边界,甚至可能是不同列的颜色编码。
采纳答案by tsvikas
You can also use the option_context
, with one or more options:
您还可以将option_context
, 与一个或多个选项一起使用:
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
This will automatically return the options to their previous values.
这将自动将选项返回到它们以前的值。
If you are working on jupyter-notebook, using display(df)
instead of print(df)
will use jupyter rich display logic (like so).
如果您正在使用 jupyter-notebook, usingdisplay(df)
而不是print(df)
将使用 jupyter 丰富的显示逻辑(像这样)。
回答by Dan Allan
Sure, if this comes up a lot, make a function like this one. You can even configure it to load every time you start IPython: https://ipython.org/ipython-doc/1/config/overview.html
当然,如果经常出现这种情况,请创建一个这样的函数。您甚至可以将其配置为每次启动 IPython 时加载:https: //ipython.org/ipython-doc/1/config/overview.html
def print_full(x):
pd.set_option('display.max_rows', len(x))
print(x)
pd.reset_option('display.max_rows')
As for coloring, getting too elaborate with colors sounds counterproductive to me, but I agree something like bootstrap's .table-striped
would be nice. You could always create an issueto suggest this feature.
至于着色,过于复杂的颜色对我来说听起来适得其反,但我同意像bootstrap 这样的.table-striped
东西会很好。您可以随时创建一个问题来建议此功能。
回答by lucidyan
After importing pandas, as an alternative to using the context manager, set such optionsfor displaying entire dataframes:
导入 pandas 后,作为使用上下文管理器的替代方法,设置用于显示整个数据帧的选项:
pd.set_option('display.max_columns', None) # or 1000
pd.set_option('display.max_rows', None) # or 1000
pd.set_option('display.max_colwidth', -1) # or 199
For full list of useful options, see:
有关有用选项的完整列表,请参阅:
pd.describe_option('display')
回答by Andrey Shokhin
No need to hack settings. There is a simple way:
无需破解设置。有一个简单的方法:
print(df.to_string())
回答by Liang Zulin
Try this
尝试这个
pd.set_option('display.height',1000)
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)
回答by R Kisyula
If you are using Ipython Notebook (Jupyter). You can use HTML
如果您使用的是 Ipython Notebook (Jupyter)。你可以使用 HTML
from IPython.core.display import HTML
display(HTML(df.to_html()))
回答by The Unfun Cat
Use the tabulate package:
使用表格包:
pip install tabulate
And consider the following example usage:
并考虑以下示例用法:
import pandas as pd
from io import StringIO
from tabulate import tabulate
c = """Chromosome Start End
chr1 3 6
chr1 5 7
chr1 8 9"""
df = pd.read_table(StringIO(c), sep="\s+", header=0)
print(tabulate(df, headers='keys', tablefmt='psql'))
+----+--------------+---------+-------+
| | Chromosome | Start | End |
|----+--------------+---------+-------|
| 0 | chr1 | 3 | 6 |
| 1 | chr1 | 5 | 7 |
| 2 | chr1 | 8 | 9 |
+----+--------------+---------+-------+
回答by Abhinav Ravi
You can achieve this using below method. just pass the total no. of columns present in the DataFrame as arg to
您可以使用以下方法实现此目的。只是通过总没有。DataFrame 中作为 arg 存在的列数
'display.max_columns'
'display.max_columns'
For eg :
例如:
df= DataFrame(..)
with pd.option_context('display.max_rows', None, 'display.max_columns', df.shape[1]):
print(df)
回答by Acumenus
Using pd.options.display
使用 pd.options.display
This answer is a variation of the prior answer by lucidyan. It makes the code more readable by avoiding the use of set_option
.
此答案是lucidyan先前答案的变体。它通过避免使用set_option
.
After importing pandas, as an alternative to using the context manager, set such optionsfor displaying large dataframes:
导入 pandas 后,作为使用上下文管理器的替代方法,设置用于显示大型数据帧的选项:
def set_pandas_display_options() -> None:
# Ref: https://stackoverflow.com/a/52432757/
display = pd.options.display
display.max_columns = 1000
display.max_rows = 1000
display.max_colwidth = 199
display.width = None
# display.precision = 2 # set as needed
set_pandas_display_options()
After this, you can use either display(df)
or just df
if using a notebook, otherwise print(df)
.
在此之后,您可以使用display(df)
或仅df
使用笔记本,否则print(df)
.
Using to_string
使用 to_string
Pandas 0.25.3 does have DataFrame.to_string
and Series.to_string
methods which accept formatting options.
熊猫0.25.3确实有DataFrame.to_string
和Series.to_string
其接受的格式选项的方法。
Using to_markdown
使用 to_markdown
If what you need is markdown output, Pandas 1.0.0 has DataFrame.to_markdown
and Series.to_markdown
methods.
如果你需要的是 Markdown 输出,Pandas 1.0.0 有DataFrame.to_markdown
和Series.to_markdown
方法。
Using to_html
使用 to_html
If what you need is HTML output, Pandas 0.25.3 does have a DataFrame.to_html
method but not a Series.to_html
. Note that a Series
can be convertedto a DataFrame
.
如果您需要的是 HTML 输出,Pandas 0.25.3 确实有一个DataFrame.to_html
方法,但没有Series.to_html
. 请注意, aSeries
可以转换为 a DataFrame
。
回答by Sabari Vishnu Jayanthan J
Try using display() function. This would automatically use Horizontal and vertical scroll bars and with this you can display different datasets easily instead of using print().
尝试使用 display() 函数。这将自动使用水平和垂直滚动条,这样您就可以轻松地显示不同的数据集,而不是使用 print()。
display(dataframe)
display() supports proper alignment also.
display() 也支持正确对齐。
However if you want to make the dataset more beautiful you can check pd.option_context()
. It has lot of options to clearly show the dataframe.
但是,如果您想让数据集更美观,您可以检查pd.option_context()
. 它有很多选项可以清楚地显示数据框。
Note - I am using Jupyter Notebooks.
注意 - 我正在使用 Jupyter Notebooks。