In the domain of data analysis and manipulation, pandas is a powerhouse library in Python. However, when working with larger datasets or complex dataframes, displaying all columns can be a challenging task. When we display the content of a pandas dataframe, pandas try to fit all the dataframe columns on the screen. As a result, it truncates the columns to fit them on the screen, especially if the dataframes are larger. However, displaying all columns of a Pandas DataFrame is a common requirement in data analysis and manipulation tasks. While Pandas provides various ways to view and manipulate data, this blog will give a clear understanding of how to effortlessly display all columns of a Pandas DataFrame without column truncations on the screen.
The Challenge of Displaying All Columns of pandas dataframe
Pandas dataframes can contain numerous columns, often making it hard to view all the information on the screen at once. To fit the dataframe on the user’s screen, the pandas may truncate columns, leading to incomplete insights and potential misunderstandings. By default, pandas only display a limited number of columns when we print the dataframe.
To demonstrate this, we can use the below line of code to generate a datframe with 20 columns with some random values and print the dataframe using print method.
# Import pandas and numpy libraries
import pandas as pd
import numpy as np
# Set the number of columns to 20
num_columns = 20
# Create a dictionary with random data for each column with the column name
data = {f'Column{i}': np.random.randint(0, 100, 5) for i in range(1, num_columns + 1)}
# Create the DataFrame using pandas
df = pd.DataFrame(data)
# Print the DataFrame
print(df)
When we execute the above print command, we get this output.
How to display All Columns of a Pandas DataFrame
Although we may achieve this in numerous ways, we will discuss some common methods here.
1. By setting the display option
Pandas provides various options to set that can be used to control the dataframe display on the screen. For example, we can use the pandas max_columns option to None to show all the columns of the dataframe on the screen. The dataframe columns may expand in multiple rows to show all the columns. Below is the command to set the max_columns option in a pandas dataframe using set_option method.
# Set the max_columns option to None in pandas
pd.set_option('display.max_columns', None)
# Print the DataFrame
print(df)
In the above code, we have used pandas set_option method to set the values of max_columns as None. This directs pandas to exhibit all columns without considering whether they fit within the available screen space. The output of the looks like this.
2. Using the to_string method
We can use the to_string() method to print all dataframe columns on the screen. However, by default, the to_string() method wraps long columns, but we can also specify a maximum column width to prevent wrapping. Below is the sample code and output of the to_string method.
print(df.to_string())
Conclusion
Displaying all columns of a pandas dataframe is essential for gaining a complete understanding of the data. This helps us to view the complete data rather than the truncated data.
Thanks for the reading. Please share your inputs in the comment section.