Explain Codes LogoExplain Codes Logo

How to show all columns' names on a large pandas dataframe?

python
pandas
dataframe
display-settings
Alex KataevbyAlex Kataev·Dec 17, 2024
TLDR

To directly display all column names in a big DataFrame, adjust pandas' display settings:

import pandas as pd # Willing to go full panorama? Set to None. pd.set_option('display.max_columns', None) # Like reading a baby's name book, but for data. print(df.columns)

This routine exposes each column name in df, cleverly dodging the truncation default.

Convert column names into a list

When you need to interact, process, or simply gain more flexibility with your column names, it's useful to convert them into a list:

# Get all column names into your Pandora's box (aka list) columns_list = df.columns.tolist()

This facilitates working with the column names for various manipulations, such as filtering or iteration:

# Detect columns with 'price' - guess who's going shopping? price_columns = [col for col in df.columns if 'price' in col]

Addressing wider-than-life dataframes

Ever hated seeing '...' in your console or notebook for huge dataframes? Well, panda solves that:

# A wider perspective on life, and data. pd.set_option('display.width', None)

Now, your output width optimally adjusts to show all columns, thereby circumventing any unwanted truncation.

Wrangling massive rows

With hefty datasets, not only the columns, but the rows might try to overwhelm you as well. Here's how to teach them who's the boss:

# Wants to show all rows? Take this! pd.set_option('display.max_rows', None)

Bolstered by this setting, you can now scrutinize all your data, row after row, unearthing patterns and recognizing outliers with utmost clarity.

Advanced tips and tricks on display settings

For those who love to dive into the deep end, here is a guide to squeeze even more juice out of pandas:

Display customization

You can conveniently tailor the display settings including max_columns, max_rows, and width:

# Tune the settings to your whims and fancies pd.options.display.max_columns = 50 # Because 50 is a nice round number pd.options.display.max_rows = 10 # Because who has time for more? pd.options.display.width = 200 # Fit for a king

These simple customizations can help make your outputs more manageable and user-friendly.

Handling column names as series

If you're a fan of pandas Series, then you've hit the jackpot:

# When life gives you columns, make a Series! columns_series = pd.Series(df.columns.values)

With columns_series, now you can effortlessly apply Series methods for filtering, sorting, and more.

Advanced configuration with pandas

Want to push boundaries? Panda's got you covered:

# Your ticket to the major league pd.set_option('display.max_info_columns', 100) pd.set_option('display.max_seq_items', None)

Get ready for a plethora of advanced options to fine-tune how your pandas dataframe behaves and displays.