Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print DataFrame on single line

Tags:

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 
like image 224
alphanumeric Avatar asked Sep 14 '16 04:09

alphanumeric


People also ask

How do I print the first 10 rows in a data frame?

Use pandas. DataFrame. head(n) to get the first n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the start).

How do I view full Dataframe in Jupyter?

To show the full data without any hiding, you can use pd. set_option('display. max_rows', 500) and pd. set_option('display.

Can you slice a Dataframe?

A Pandas Series is a one-dimensional labeled numpy array and a dataframe is a two-dimensional numpy array whose columns are Series. DataFrames can therefore be Sliced just like numpy arrays or Python lists.

How do I print a single value from a Dataframe?

You can get the value of a cell from a pandas dataframe using df. iat[0,0] .


1 Answers

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.

like image 136
jezrael Avatar answered Oct 08 '22 00:10

jezrael