Skip to content

Show All Columns and Rows in a Pandas DataFrame

Pandas Show All Columns and Rows Cover Image

In this tutorial, you’ll learn how to change your display options in Pandas to display all columns, as well as all rows in your DataFrame. By default, Pandas will limit the number of columns and rows to display. While this can be helpful in terms of being able to read content, it can often lead to frustration if the data you want to see isn’t displayed.

By the end of this tutorial, you’ll have learned:

  • How to identify the default values for number of columns and rows Pandas will show
  • How to show all columns and/or rows in a Pandas DataFrame
  • How to specify the number of columns and/or rows to display from a Pandas DataFrame
  • How to reset the display values to their default

Loading a Sample Pandas DataFrame

To follow along with this tutorial line-by-line, feel free to copy and paste the code below to load a sample Pandas DataFrame. We’ll use the Seaborn load_dataset() function and load the 'brain_networks' dataset. If you have your own data, feel free to use that.

import pandas as pd
from seaborn import load_dataset

df = load_dataset('brain_networks')
print(df)

# Returns:
#     network                    1                  1.1  ...                 17.4                17.5                 17.6
# 0      node                    1                    1  ...                    3                   3                    4
# 1      hemi                   lh                   rh  ...                   lh                  rh                   lh
# 2       NaN                  NaN                  NaN  ...                  NaN                 NaN                  NaN
# 3         0    56.05574417114258    92.03103637695312  ...  -10.520872116088867  120.49046325683594  -39.686431884765625
# 4         1     55.5472526550293     43.6900749206543  ...  -39.607521057128906   24.76401138305664    -36.7710075378418
# ..      ...                  ...                  ...  ...                  ...                 ...                  ...
# 918     915  -7.4295125007629395    -4.81321907043457  ...   22.893030166625977   48.27437973022461    76.22845458984375
# 919     916   -33.55413818359375   -38.60562133789063  ...    24.97454833984375   51.97215270996094    64.53878784179689
# 920     917   -78.53956604003906   -74.19718933105469  ...    66.99440002441406   81.53924560546875    64.96977233886719
# 921     918  -103.23582458496094    -98.7442855834961  ...   20.517745971679688   3.124434232711792    56.71838760375977
# 922     919   -36.28886795043945  -10.762069702148438  ...    8.300398826599121  33.687530517578125   17.960655212402344

# [923 rows x 63 columns]

We can see that by printing out the DataFrame that both the column axis and the row axis have been truncated. Let’s begin by exploring how to determine how many rows and columns Pandas will display by default.

Finding the Default Values for Number of Columns/Rows Pandas Displays

In order to display the number of rows and columns that Pandas displays by default, we can use the .get_option() function. This function takes a value and returns the provided option for that value.

In this case, we’re looking for two particular options:

  • 'display.max_columns', which controls the number of columns to display
  • 'display.max_rows', which controls the number of rows to display

Let’s take a look at what the maximum number of columns our script will display is:

# Identifying the max number of columns to display
print(pd.get_option('display.max_rows'))

# Returns: 0

The value this returned was 0. Zero, in this case, is a special case. What this tells Pandas is to automatically try and find the best fit based on the size of your terminal.

How to Show all Columns in a Pandas DataFrame

In this section, you’ll learn how to display all the columns of your Pandas DataFrame. In order to do this, we can use the pd.set_option() function. Similar to the example above, we want to set the display.max_columns option.

In order to display all columns in Pandas, we must set this option to be None. Let’s see how we can do this:

pd.set_option('display.max_columns', None)
print(df.head(2))

# Returns:
#   network   1 1.1   2 2.1   3 3.1   4 4.1   5 5.1   6 6.1 6.2 6.3   7 7.1 7.2  \
# 0    node   1   1   1   1   1   1   1   1   1   1   1   1   2   2   1   1   2   
# 1    hemi  lh  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh   

#   7.3 7.4 7.5   8 8.1 8.2 8.3 8.4 8.5   9 9.1  10 10.1  11 11.1  12 12.1 12.2  \
# 0   2   3   3   1   1   2   2   3   3   1   1   1    1   1    1   1    1    2   
# 1  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh  rh  lh   rh  lh   rh  lh   rh   lh   

#   12.3 12.4  13 13.1 13.2 13.3 13.4 13.5  14 14.1  15 15.1  16 16.1 16.2 16.3  \
# 0    2    3   1    1    2    2    3    4   1    1   1    1   1    1    2    2   
# 1   rh   lh  lh   rh   lh   rh   rh   rh  lh   rh  lh   rh  lh   rh   lh   rh   

#   16.4 16.5 16.6 16.7  17 17.1 17.2 17.3 17.4 17.5 17.6  
# 0    3    3    4    4   1    1    2    2    3    3    4  
# 1   lh   rh   lh   rh  lh   rh   lh   rh   lh   rh   lh  

In this case, Pandas will continue to wrap columns, even if they split across multiple lines.

Another way in which we can accomplish this is to use direct assignment. We can do this by assigning a value directly to the option, as shown below:

# Setting the display option directly
pd.options.display.max_columns = None

How to Specify the Number of Columns to Show in a Pandas DataFrame

We can use the exact same method to force Pandas to display a set number of columns. This can be done by passing in a specific number of columns into the option.

For example, if we wanted to display 8 columns, we could use either of the two methods below:

# Specifying the number of columns to display
pd.set_option('display.max_columns', 8)

# Or
# pd.set_option.display.max_columns = 8

print(df.head(2))

# Returns:
#   network   1 1.1   2  ... 17.3 17.4 17.5 17.6
# 0    node   1   1   1  ...    2    3    3    4
# 1    hemi  lh  rh  lh  ...   rh   lh   rh   lh

# [2 rows x 63 columns]

How to Show all Rows in a Pandas DataFrame

In other cases, you may want to display a different number of rows by default. This can be done in the exact same way as above, except we will be specifying the display.max_rows option. Because of this, we can use either of the options below:

# Specifying the number of rows to display
pd.set_option('display.max_rows', None)

# Or
# pd.set_option.display.max_rows = None

I won’t print the rows out here, given the size of the dataset.

How to Specify the Number of Rows to Show in a Pandas DataFrame

Similar to the example above, we can easily specify the number of rows to display. Generally, Pandas will display the first five rows and the last five rows. We can modify this behavior by overwriting the display values and specifying a specific value.

This behavior works a little differently, however, as we also need to set a minimum number of rows to display.

Let’s take a look at an example to display 20 rows, by default:

# Display 20 rows in a DataFrame
pd.set_option('display.max_rows', 200)
pd.set_option('display.min_rows', 20)

How to Reset Your Display Options in Pandas

In order to reset the display options, Pandas provides a number of aptly-named functions. We can use the pd.reset_option() function.

In order to reset the number of columns we want to display, we can use the following code:

# Resetting the number of columns to display
pd.reset_option('display.max_columns')

Similarly, in order to reset the number of rows to display, we can use the following code:

# Resetting the number of rows to display
pd.reset_option('display.max_rows')

We can also reset all the options at once using regular expressions:

# Resetting all display options at once
pd.reset_option("^display")

Conclusion

In this post, you learned how to display all columns or rows in a Pandas DataFrame. You learned how to change the display settings in Pandas to specify how many columns and rows to display. You also learned how to reset these values to their default values.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.