How can I print a df in the Terminal without loosing the format?
Lets say I have a df like this:
In: df
Out:
TFs No Esenciales Genes regulados Genes Regulados Positivamente Genes Regulados Negativamente No Tentativo de genes a silenciar No Real de genes a silenciar No Tentativo de genes a inducir
146 YdeO 20 18 2 2 2 0
But when I use print to display it in the shell, It looses its format
In: print (df)
Out:
TFs No Esenciales Genes regulados Genes Regulados Positivamente \
146 YdeO 20 18
Genes Regulados Negativamente No Tentativo de genes a silenciar \
146 2 2
No Real de genes a silenciar No Tentativo de genes a inducir \
146 2 0
No Real de genes a inducir Balance de genes Balance real de genes
146 0 2 2
How can I use print, but keep the format?
My desired output is:
In: print (df)
Out:
TFs No Esenciales Genes regulados Genes Regulados Positivamente Genes Regulados Negativamente No Tentativo de genes a silenciar No Real de genes a silenciar No Tentativo de genes a inducir
146 YdeO 20 18 2 2 2 0
DOCUMENTATION
There are 2 things going on that control for the formatting you may see.
Controlling for the the character width that the display can handle.
display.width
and can be seen withprint pd.get_option('display.width')
. The default is80
.The second control is the number of columns in the dataframe to display.
display.max_columns
and can be seen withprint pd.get_option('display.max_columns')
. The default is20
.display.width
Let's explore what this does with a sample dataframe
pd.set_option('display.width', 40)
pd.set_option('display.width', 120)
This should scroll to the right.
display.max_columns
Let's put
'display.width'
back to 80 withpd.set_option('display.width,80)
Now let's explore different values of
'display.max_columns'
Notice the ellipses in the middle. There are 40 columns in this data frame, to get to a display count of 20 max columns, pandas took the first 10 columns
0:9
and the last 10 columns30:39
and put an ellipses in the middle.pd.set_option('display.max_columns', 30)
Notice the width of characters stayed the same but I have more columns. pandas took the first 15 columns
0:14
and the last 15 columns26:39
.To get all of your columns displayed, you need to set this option to be at least as big as the number of columns you want displayed.
pd.set_option('display.max_columns', 40)
No ellipses, all columns are displayed.
Combining both options together
Pretty simple at this point.
pd.set_option('display.width', 1000)
use 1000 to allow for something long.pd.set_option('display.max_columns', 1000)
also allowing for wide dataframes.Using your data
BIG CAVEAT
When you run this, you may not see this scrolling magic that you do here. This is because your terminal probably doesn't scroll to the right. Below is a screen shot from jupyter-notebook. It doesn't look right because the text is being wrapped. However, there are no new lines in the string where it wraps as evidenced by the fact that when I copied and pasted it to stack overflow, it displays appropriately.
There are display options that can be used to control how the
DataFrame
will be printed. You probably want: