Is there a concise way to show all lines in pandas for the current command only?

Sometimes I want to show all rows in a pandas DataFrame, but only for one command or code block.

Of course, I can set the "max_rows" display option to a large number, but then I need to repeat the command to return to my preferred setting. (I like 12 lines maximum, personally).

pd.options.display.max_rows=1000
myDF
pd.options.display.max_rows=12

      

This is annoying.

I read in the documentation that I can use the pd.option_context () function to accomplish this if I combine my command with the "c" operator:

with pd.option_context("display.max_rows", 1000): myDF

      

I couldn't get this to work, (no output is returned). But I think that such a solution would still be too complicated for typical casual use!

I wish there was some quick pythonic way to override display options!
Is there one? Did I miss something?

I like how you can change the # of lines that the .head () function outputs by passing it an argument for # of lines, but it should still be lower than the display.max_rows parameter ...

I know that I could constantly set the display.max_rows parameter to all the time and then click on the .head (12) function most of the time, but I think most people would agree with how annoying it is.

I do know that it is possible to view all (or most?) Of the value in the pandas series by passing it to a main function like list (). But this is difficult to do with DF. Also, it is difficult to read if it is not in tabular format.

As per the solution for my first question , I am guessing that there is perhaps a way to write my own function (to be hosted in a startup script), but I'm not sure if this is the best way to write.

+4


source to share


4 answers


You can write a function that explicitly calls display

For example, consider this function:

from IPython.display import display

def show_more(df, lines):
    foo = 1
    display(df)
    foo = 2

      



When I call the function (just tried it):

>> show_more(df, 1000)
... # <- Shows here the DF

      

then it displays the dataframe even if the line foo = 2

is executed after. It follows that you can set parameters instead of a string foo = 1

and remove it in the string foo = 2

. Actually, you can just use the context manager from your question.

0


source


This will display nothing because it returns nothing:

with pd.option_context("display.max_rows", 1000): myDF

      



The call display

inside the block with

should work:

with pd.option_context("display.max_rows", 1000):
    display(myDF)

      

+4


source


This seems to work as expected in pandas 0.22.0 (imports only pandas without IPython):

import pandas as pd    
with pd.option_context("display.max_rows", 1000): myDF

      

Presumably because the default behavior is to return repr myDF. IDEs can override this.

If it's too much to print, then direct printing to the terminal also works when wrapped in a function:

from __future__ import print_statement  # for python2

def show_rows(df, nrows=1000):
    with pd.option_context("display.max_rows", nrows): print(df)

      

+1


source


That's what I'm doing:

df.head(df.shape[0])

Note. If you are trying to remove ellipses from the middle of a long data frame, refer to other answers. This is just a shorthand way of using head to display everything, instead of using it IPython.display

.

0


source







All Articles