1

Entering pandas set_option('display.max_columns, None) causes the Python print function to print just some of the columns, then, below that, come back and print the rest of the columns.

I downloaded https://media.geeksforgeeks.org/wp-content/uploads/nba.csv and loaded it to a dataframe and printed it (as below)

import pandas as pd

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

data = pd.read_csv(r"D:\DriveD\Study\Python\Files\nba.csv", index_col ="Name")
print()
print(data)
print()

It prints the 'Name' column and the next 6 columns, then under those 10 rows, it prints the 'Name' columns again, along with the corresponding last two columns, for those same 10 rows.

I can redo the 'set_option' entry as below:

panda.set_option('display.max_rows', None,'display.max_colwidth', None)

and it prints the 'Name' along with the other 8 columns on one line, for all 457 rows.

SO - if I don't do the pd.setoption('display.max_columns, None) then it prints AS EXPECTED, i.e., each row is printed on one line.

I'm using VS Code, Python version 3.13.3. Windows 10.

3
  • what is the problem? What is the question? Commented Jun 20 at 11:20
  • if you want make sure you get all columns in one row then you may use print( data.to_string() ) Commented Jun 20 at 11:37
  • the 'problem' is that it prints all the columns on one line using the default value for 'display.max_columns', but if I actually set it to 'None' then it decides that it can't print all the columns on one line. Thanks for the print( data.to_string() ) advice. Commented Jun 20 at 23:13

2 Answers 2

0

I can reproduce your described behaviour on Jupyter. Even if I set 8 columns instead None It shows only 5 columns in opposite to to_string(). If I use IPython.display it shows all.

import pandas as pd
from IPython.display import display

# Set display options
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

# Load the data from a local file
data = pd.read_csv(r'https://media.geeksforgeeks.org/wp-content/uploads/nba.csv', index_col="Name")

print(display(data))

Output: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

It's a common "issues" across different environments. Whether you are using VSCode or Jupyter, you're likely to encounter it. Using pd.set_option('display.max_columns', None) is correct becausecause you're telling the library to show all columns, regardless of how many there are.

But here's the catch: pandas will display as many columns as fit within the current console width (either the default or what was set before). If it can’t fit all columns horizontally, it will wrap the output into blocks per row set, printing a subset of columns in the first block and the remaining columns in a second block with the same index.

To prevent this wrapping and force each row to appear on one line, you can also add this line:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.