我经常在终端上使用Series和DataFrames。Series的默认__repr__返回一个减少的样本,其中有一些头部和尾部值,但其余的都没有。
是否有一种内置的方式来漂亮地打印整个系列/数据帧?理想情况下,它应该支持适当的对齐,可能是列之间的边界,甚至可能是不同列的颜色编码。
我经常在终端上使用Series和DataFrames。Series的默认__repr__返回一个减少的样本,其中有一些头部和尾部值,但其余的都没有。
是否有一种内置的方式来漂亮地打印整个系列/数据帧?理想情况下,它应该支持适当的对齐,可能是列之间的边界,甚至可能是不同列的颜色编码。
当前回答
试试这个
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)
其他回答
你可以将expand_frame_repr设置为False:
显示。Expand_frame_repr:布尔值 是否打印宽DataFrame的完整DataFrame repr 跨多行,max_columns仍然得到尊重,但是输出 如果它的宽度超过了,会在多个“页面”上环绕吗 display.width。 (默认值:真实)
pd.set_option('expand_frame_repr', False)
要了解更多细节,请阅读如何漂亮地打印熊猫数据框架和系列
导入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', None) # or 199
有关有用选项的完整列表,请参见:
pd.describe_option('display')
脚本
没有人提出过这种简单的纯文本解决方案:
from pprint import pprint
pprint(s.to_dict())
产生如下结果:
{'% Diabetes': 0.06365372374283895,
'% Obesity': 0.06365372374283895,
'% Bachelors': 0.0,
'% Poverty': 0.09548058561425843,
'% Driving Deaths': 1.1775938892425206,
'% Excessive Drinking': 0.06365372374283895}
Jupyter記本嗎
此外,当使用Jupyter笔记本时,这是一个很好的解决方案。
注意:pd.Series()没有.to_html(),所以必须转换为pd.DataFrame()
from IPython.display import display, HTML
display(HTML(s.to_frame().to_html()))
产生如下结果:
试试这个
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)
datasroller的创建部分是为了解决这个问题。
pip install datascroller
它将数据帧加载到终端视图中,你可以用鼠标或方向键“滚动”,有点像终端上的Excel工作簿,支持查询、高亮显示等。
import pandas as pd
from datascroller import scroll
# Call `scroll` with a Pandas DataFrame as the sole argument:
my_df = pd.read_csv('<path to your csv>')
scroll(my_df)
披露:我是datascroller的作者之一