如何在单行上打印DataFrame [英] How to print DataFrame on single line

查看:280
本文介绍了如何在单行上打印DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:

import pandas as pd
df = pd.read_csv('pima-data.csv')
print df.head(2)

打印内容自动跨多行设置格式:

the print is automatically formatted across multiple lines:

   num_preg  glucose_conc  diastolic_bp  thickness  insulin   bmi  diab_pred  \
0         6           148            72         35        0  33.6      0.627   
1         1            85            66         29        0  26.6      0.351   

   age    skin diabetes  
0   50  1.3790     True  
1   31  1.1426    False 

我想知道是否有一种避免多行格式的方法.我希望将其打印在这样的一行中:

I wonder if there is a way to avoid the multi-line formatting. I would rather have it printed in a single line like so:

   num_preg  glucose_conc  diastolic_bp  thickness  insulin       bmi      diab_pred     age       skin      diabetes  
0         6           148            72         35        0      33.6          0.627      50     1.3790          True  
1         1            85            66         29        0      26.6          0.351      31     1.1426         False 

推荐答案

您需要设置:

pd.set_option('expand_frame_repr', False)

option_context上下文管理器已通过顶级API公开,您可以使用给定的选项值执行代码.退出with块时,选项值将自动恢复:

option_context context manager has been exposed through the top-level API, allowing you to execute code with given option values. Option values are restored automatically when you exit the with block:

#temporaly set expand_frame_repr
with pd.option_context('expand_frame_repr', False):
    print (df)

熊猫文档.

这篇关于如何在单行上打印DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆