How to print a DataFrame on one line

WITH

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

Printing is automatically formatted in several 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 multi-line formatting. I would prefer it to print on one line like this:

   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 
+4
source share
1 answer

You need to install:

pd.set_option('expand_frame_repr', False)

option_contextThe context manager was opened through the top-level API, which allows you to execute code with the specified parameter values. The parameter values ​​are automatically restored when you exit the block:

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

Pandas documentation .

+6
source

Source: https://habr.com/ru/post/1654522/


All Articles