Getting wider output in PyCharm's built-in console

Question:

I’m relatively new to using the PyCharm IDE, and have been unable to find a way to better shape the output when in a built-in console session. I’m typically working with pretty wide dataframes, that would fit easily across my monitor, but the display is cutting and wrapping them much sooner than needed.

Does anyone know of a setting to change this behavior to take advantage of the full width of my screen?

Edit: I don’t have enough reputation to post a screenshot, but link is below:
http://imgur.com/iiBK3iU

I would like to prevent it from wrapping after only a few columns (for example, the column ‘ReadmitRate’ should be immediately to the right of ‘SNFDaysPerSNFCase’)

Asked By: mattvivier

||

Answers:

It appears I was mistaken in thinking that the issue was one in PyCharm (that could be solved, for example, in a setting or preference.) It actually has to do with the console session itself. The console attempts to auto-detect the width of the display area, but when that fails it defaults to 80 characters. This behavior can be overridden with:

import pandas as pd
desired_width = 320    
pd.set_option('display.width', desired_width)

Where you can of course set the desired_width to whatever your display will tolerate.
Thanks to @TidB for the suggestion that my initial concern wasn’t focused in the right area.

Answered By: mattvivier

The answer by @mattvivier works nicely when printing Pandas dataframes (thanks!).

However, if you are printing NumPy arrays, you need to set np.set_printoptions as well:

import pandas as pd
import numpy as np
desired_width = 320
pd.set_option('display.width', desired_width)
np.set_printoptions(linewidth=desired_width)

See docs on NumPy and set_printoptions.

Answered By: Contango

For me, just setting 'display.width' wasn’t enough in pycharm, it kept displaying in truncated form.

However, adding the option pd.set_option("display.max_columns", 10) together with display width worked and I was able to see the whole dataframe printed in the “run” output.

In summary:

import pandas as pd    
pd.set_option('display.width', 400)
pd.set_option('display.max_columns', 10)
Answered By: Idodo
def pd_set_df_view_options(max_rows=1000, max_columns=350, display_width=320):

    # Show more than 10 or 20 rows when a dataframe comes back.
    pd.set_option('display.max_rows', max_rows) 
    # Columns displayed in debug view
    pd.set_option('display.max_columns', max_columns)

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

# run
pd_set_df_view_options(max_rows=1000, max_columns=350, display_width=320)
Answered By: gies0r

Adding to Contago’s answer :

Universal settings

import numpy as np
import pandas as pd


desired_width = 320

## dataframe ##
# increase dataframe print area
pd.set_option('display.width', desired_width)

# increase no. of df columns displayed
pd.set_option('display.max_columns', n)


## numpy array ##
# increase numpy array print area
np.set_printoptions(linewidth = desired_width)


Context specific (temporary)

# dataframe
with pd.option_context('display.max_columns', 20,
                       'display.width', 320):
    print(df)

# numpy array
with np.printoptions(linewidth=320):
    print(np.array(df))
Answered By: rahul-ahuja
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.