How to print DataFrame on single line

11,805

You need set:

pd.set_option('expand_frame_repr', False)

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)

Pandas documentation.

Share:
11,805

Related videos on Youtube

alphanumeric
Author by

alphanumeric

Updated on September 15, 2022

Comments

  • alphanumeric
    alphanumeric over 1 year

    With:

    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 
    
  • jezrael
    jezrael over 7 years
    Glad can help you. Nice day!
  • Joe T. Boka
    Joe T. Boka over 7 years
    It doesn't seem to work on Spyder. I wonder if I should change some settings on Spyder, when I try this pd.set_option there is nor scroll bar so it becomes kind of a mess. Any suggestions for Spyder?
  • jezrael
    jezrael over 7 years
    @JoeR - I check settings in IPython console and only you can change Size of font, but I dont find options for changing this. If check docs it seems you can use only set_option.
  • Joe T. Boka
    Joe T. Boka over 7 years
    Thanks. Yes, I've been trying to make this work on Spyder for about a week but no luck. I tried all set_option variations but the result is always the same. Thanks again for your reply. I appreciate it.