Open In App

How to Show All Columns of a Pandas DataFrame?

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame.

Display-DataFrame

Using pd.set_option to Show All Pandas Columns

Pandas provides a set_option() function that allows you to configure various display options, including the number of columns to display.

Python
import pandas as pd
df = pd.read_csv('train.csv')

# Set display option to show all columns
pd.set_option('display.max_columns', None)

# Show the columns 
display(df)

Output:

All-columns-in-Pandas-DataFrame
Pandas DataFrame with All the Columns

Using df.columns to List All Column Names

If you don't need to view the entire DataFrame but just want to know the column names, you can use the df.columns attribute. This returns an index object containing all the column names.

Python
import pandas as pd

# Read the CSV file into a DataFrame
df = pd.read_csv('train.csv')

# Print all column names
print(df.columns)

Output:

Index(['Id', 'MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street',
'Alley', 'LotShape', 'LandContour', 'Utilities', 'LotConfig',
'LandSlope', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType',
'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'MasVnrArea', 'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual',
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1',
'BsmtFinType2', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'Heating',
'HeatingQC', 'CentralAir', 'Electrical', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath',
'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',
'TotRmsAbvGrd', 'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType',
'GarageYrBlt', 'GarageFinish', 'GarageCars', 'GarageArea', 'GarageQual',
'GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF',
'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'PoolQC',
'Fence', 'MiscFeature', 'MiscVal', 'MoSold', 'YrSold', 'SaleType',
'SaleCondition', 'SalePrice'],
dtype='object')

This method is especially useful when you're debugging or need a quick overview of the columns in the DataFrame.

Using to_string() to Display All Columns and Rows

If you need to view the entire DataFrame, including all rows and columns, use to_string(). This method converts the DataFrame into a string representation, allowing you to view everything at once.

Python
import pandas as pd

# Read the CSV file into a DataFrame
df = pd.read_csv('train.csv')

# Display all rows and columns
print(df.to_string())

Output:

Display-all-columns-and-rows

Be cautious when using this with large datasets, as it can produce a lot of output in the console.


Next Article

Similar Reads