Skip to content

Beginners' Guide to Easily Viewing All Pandas Columns

[

Pandas: Seeing All Columns

Summary

Pandas is a powerful data manipulation tool that provides various functionalities for handling and analyzing structured data. One common task is to view all columns in a pandas DataFrame, which can be achieved using several different methods. In this tutorial, we will explore these methods in detail and provide a step-by-step guide, including executable sample code.

Introduction

Working with large datasets often requires examining the complete set of available columns. Pandas offers multiple ways to achieve this, allowing users to easily inspect and analyze their data. In this tutorial, we will cover ten different methods to see all columns in a pandas DataFrame, providing a comprehensive range of options to suit various needs.

Method 1: Using the .columns attribute

To view all columns in a DataFrame, we can simply access the .columns attribute. This attribute returns a pandas Index object representing the column names.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
print(df.columns)

Output:

Index(['A', 'B', 'C'], dtype='object')

Method 2: Using the .info() method

The .info() method provides a concise summary of the DataFrame, including column names, data types, and non-null values. This is a convenient way to quickly obtain an overview of the dataset.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df.info()

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
A 3 non-null int64
B 3 non-null int64
C 3 non-null int64
dtypes: int64(3)
memory usage: 200.0 bytes

Method 3: Using the .head() method with a large number of columns

By default, the .head() method displays the first five rows of a DataFrame. However, when dealing with many columns, it may truncate the output. To view all columns, we can adjust the display options using .set_option().

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
pd.set_option('display.max_columns', None) # Display all columns
print(df.head())

Output:

A B C
0 1 4 7
1 2 5 8
2 3 6 9

Method 4: Using the .drop() method with a column parameter

The .drop() method is commonly used to remove columns from a DataFrame. However, by passing an empty column parameter, we can drop all columns except the index.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df.drop(columns=df.columns, inplace=True)
print(df)

Output:

Empty DataFrame
Columns: []
Index: [0, 1, 2]

Method 5: Using the .iloc[:, :] indexing

The .iloc indexer allows for precise selection of DataFrame elements by integer location. We can use this method with a slice (:) notation to select all rows and all columns, effectively displaying the full DataFrame.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
print(df.iloc[:, :])

Output:

A B C
0 1 4 7
1 2 5 8
2 3 6 9

Method 6: Using the .T property

The .T property transposes the DataFrame, converting the rows into columns and vice versa. This can be useful to view all columns as rows, providing a different perspective on the data.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
print(df.T)

Output:

0 1 2
A 1 2 3
B 4 5 6
C 7 8 9

Method 7: Using the .style.set_table_styles() method

The .style.set_table_styles() method allows us to customize the appearance of a DataFrame. By applying a style that displays all columns, we can view the complete dataset.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
pd.set_option('display.max_columns', None) # Display all columns
style = [{'selector': 'th,td', 'props': [('max-width', '50px')]}]
df.style.set_table_styles(style)
print(df)

Output:

A B C
0 1 4 7
1 2 5 8
2 3 6 9

Method 8: Using the .to_string() method

The .to_string() method provides a string representation of the DataFrame, allowing us to see all columns and rows in a compact format.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
print(df.to_string())

Output:

A B C
0 1 4 7
1 2 5 8
2 3 6 9

Method 9: Using the .set_printoptions() function

The .set_printoptions() function allows us to modify the way pandas displays data. By setting the threshold parameter to a large value, we can ensure that all columns are displayed in the output.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 100)))
pd.set_option('display.max_columns', None) # Display all columns
np.set_printoptions(threshold=np.inf) # Display all rows
print(df)

Output:

0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
0 7 9 3 5 2 3 6 0 0 1 ... 5 9 7 4 9 5 0 9 8 0
1 2 9 1 3 6 6 9 9 7 2 ... 2 0 7 9 0 5 6 7 5 8
2 9 4 1 4 1 2 1 8 2 4 ... 7 8 7 4 3 6 8 5 1 8
3 2 9
[![](/banner-2.png)](/python/)