如何在单行上打印DataFrame

alp*_*ric 9 python dataframe pandas

附:

import pandas as pd
df = pd.read_csv('pima-data.csv')
print df.head(2)
Run Code Online (Sandbox Code Playgroud)

打印自动格式化为多行:

   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 
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法避免多行格式化.我宁愿把它打印成一行,如下所示:

   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 
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 12

你需要设置:

pd.set_option('expand_frame_repr', False)
Run Code Online (Sandbox Code Playgroud)

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

#temporaly set expand_frame_repr
with pd.option_context('expand_frame_repr', False):
    print (df)
Run Code Online (Sandbox Code Playgroud)

熊猫文档.