python - How to print a df in Terminal without loosing format? -


how can print df in terminal without loosing format?

lets have df this:

in: df out:      tfs no esenciales  genes regulados  genes regulados positivamente  genes regulados negativamente  no tentativo de genes silenciar  no real de genes silenciar  no tentativo de genes inducir 146              ydeo               20                             18                              2                              2                               2                               0 

but when use print display in shell, looses format

in: print (df) out:         tfs no esenciales  genes regulados  genes regulados positivamente  \ 146              ydeo               20                             18          genes regulados negativamente  no tentativo de genes silenciar  \ 146                              2                                 2          no real de genes silenciar  no tentativo de genes inducir  \ 146                            2                               0          no real de genes inducir  balance de genes  balance real de genes   146                          0                 2                      2   

how can use print, keep format?

my desired output is:

in: print (df)     out:      tfs no esenciales  genes regulados  genes regulados positivamente  genes regulados negativamente  no tentativo de genes silenciar  no real de genes silenciar  no tentativo de genes inducir 146              ydeo               20                             18                              2                              2                               2                               0 

documentation

there 2 things going on control formatting may see.

  1. controlling the character width display can handle.

    • this handled pandas option display.width , can seen print pd.get_option('display.width'). default 80.
  2. the second control number of columns in dataframe display.

    • this handled pandas option display.max_columns , can seen print pd.get_option('display.max_columns'). default 20.

display.width

let's explore sample dataframe

import pandas pd  df = pd.dataframe([range(40)], columns=['abcde%d' % in range(40)])  print df # default 'display.width' of 80     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  \ 0       0       1       2       3       4       5       6       7       8        abcde9   ...     abcde30  abcde31  abcde32  abcde33  abcde34  abcde35  \ 0       9   ...          30       31       32       33       34       35        abcde36  abcde37  abcde38  abcde39   0       36       37       38       39    [1 rows x 40 columns] 

pd.set_option('display.width', 40)

print df     abcde0  abcde1  abcde2  abcde3  \ 0       0       1       2       3        abcde4  abcde5  abcde6  abcde7  \ 0       4       5       6       7        abcde8  abcde9   ...     abcde30  \ 0       8       9   ...          30        abcde31  abcde32  abcde33  abcde34  \ 0       31       32       33       34        abcde35  abcde36  abcde37  abcde38  \ 0       35       36       37       38        abcde39   0       39    [1 rows x 40 columns] 

pd.set_option('display.width', 120)

this should scroll right.

print df     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  abcde9   ...     abcde30  abcde31  abcde32  \ 0       0       1       2       3       4       5       6       7       8       9   ...          30       31       32        abcde33  abcde34  abcde35  abcde36  abcde37  abcde38  abcde39   0       33       34       35       36       37       38       39    [1 rows x 40 columns] 

display.max_columns

let's put 'display.width' 80 pd.set_option('display.width,80)

now let's explore different values of 'display.max_columns'

print df # default 20     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  \ 0       0       1       2       3       4       5       6       7       8        abcde9   ...     abcde30  abcde31  abcde32  abcde33  abcde34  abcde35  \ 0       9   ...          30       31       32       33       34       35        abcde36  abcde37  abcde38  abcde39   0       36       37       38       39    [1 rows x 40 columns] 

notice ellipses in middle. there 40 columns in data frame, display count of 20 max columns, pandas took first 10 columns 0:9 , last 10 columns 30:39 , put ellipses in middle.

pd.set_option('display.max_columns', 30)

print df     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  \ 0       0       1       2       3       4       5       6       7       8        abcde9  abcde10  abcde11  abcde12  abcde13  abcde14   ...     abcde25  \ 0       9       10       11       12       13       14   ...          25        abcde26  abcde27  abcde28  abcde29  abcde30  abcde31  abcde32  abcde33  \ 0       26       27       28       29       30       31       32       33        abcde34  abcde35  abcde36  abcde37  abcde38  abcde39   0       34       35       36       37       38       39    [1 rows x 40 columns] 

notice width of characters stayed same have more columns. pandas took first 15 columns 0:14 , last 15 columns 26:39.

to of columns displayed, need set option @ least big number of columns want displayed.

pd.set_option('display.max_columns', 40)

print df     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  \ 0       0       1       2       3       4       5       6       7       8        abcde9  abcde10  abcde11  abcde12  abcde13  abcde14  abcde15  abcde16  \ 0       9       10       11       12       13       14       15       16        abcde17  abcde18  abcde19  abcde20  abcde21  abcde22  abcde23  abcde24  \ 0       17       18       19       20       21       22       23       24        abcde25  abcde26  abcde27  abcde28  abcde29  abcde30  abcde31  abcde32  \ 0       25       26       27       28       29       30       31       32        abcde33  abcde34  abcde35  abcde36  abcde37  abcde38  abcde39   0       33       34       35       36       37       38       39   

no ellipses, columns displayed.

combining both options together

pretty simple @ point. pd.set_option('display.width', 1000) use 1000 allow long. pd.set_option('display.max_columns', 1000) allowing wide dataframes.

print df     abcde0  abcde1  abcde2  abcde3  abcde4  abcde5  abcde6  abcde7  abcde8  abcde9  abcde10  abcde11  abcde12  abcde13  abcde14  abcde15  abcde16  abcde17  abcde18  abcde19  abcde20  abcde21  abcde22  abcde23  abcde24  abcde25  abcde26  abcde27  abcde28  abcde29  abcde30  abcde31  abcde32  abcde33  abcde34  abcde35  abcde36  abcde37  abcde38  abcde39 0       0       1       2       3       4       5       6       7       8       9       10       11       12       13       14       15       16       17       18       19       20       21       22       23       24       25       26       27       28       29       30       31       32       33       34       35       36       37       38       39 

using data

print df     tfs    no  esenciales  genes  regulados  genes.1  regulados  positivamente  genes.2  regulados.1  negativamente  no.1  tentativo  de  genes    silenciar  no.2  real  de.1  genes.1  a.1  silenciar.1  no.3  tentativo.1  de.2  genes.2  a.2  inducir 0  146  ydeo          20     18          2        2          2              0      nan          nan            nan   nan        nan nan    nan nan        nan   nan   nan   nan      nan  nan          nan   nan          nan   nan      nan  nan      nan 

big caveat

when run this, may not see scrolling magic here. because terminal doesn't scroll right. below screen shot jupyter-notebook. doesn't right because text being wrapped. however, there no new lines in string wraps evidenced fact when copied , pasted stack overflow, displays appropriately.

enter image description here


Comments