Sling Academy
Home/Pandas/Pandas: How to print all columns of a huge DataFrame

Pandas: How to print all columns of a huge DataFrame

Last updated: February 20, 2024

Overview

When working with large datasets in Python’s Pandas library, printing the entire DataFrame to view all columns can be challenging due to the default truncate view. This tutorial will guide you through several methods to print all columns of a huge DataFrame, allowing you to fully inspect your data without missing any part. We will start with the most basic methods and gradually move to more advanced techniques.

Understanding Pandas Settings

Pandas’ display settings control how DataFrames are shown. By default, Pandas will not display all columns if a DataFrame has a large number of columns. Instead, it shows a truncated version. To change this behavior, we need to understand the pd.set_option functionality.

import pandas as pd
# Create a sample large DataFrame
df = pd.DataFrame({'A': range(1, 101), 'B': range(101, 201), ..., 'Z': range(2601, 2701)})
# Default print will truncate columns

Printing All Columns

To print all columns of a DataFrame, you can adjust the 'display.max_columns' option. This forces Pandas to display every column, no matter the DataFrame’s size.

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

This method allows you to see all the columns when you print the DataFrame.

Using to_string() Method

The to_string() method is another way to print the entire DataFrame. It converts the DataFrame to a string and then you can print it. By doing so, you circumvent the default settings that truncate the display.

print(df.to_string())

While this method ensures that all columns are displayed, it might not be suitable for DataFrames with a vast number of rows, as it will attempt to print everything, potentially overwhelming your console.

Viewing in Chunks

If you have a truly massive DataFrame and printing it all at once is not practical, you can view it in chunks. This involves manually specifying the columns you want to view at any one time. While not as convenient as setting options, it gives you ultimate control.

cols_to_view = ['A', 'B', 'C']
print(df[cols_to_view].to_string())

You can change cols_to_view to include different sets of columns as needed.

Advanced Viewing with Transposing

For DataFrames where it’s the row dimension that’s unwieldy, transposing the DataFrame (swapping rows and columns) and then applying the above methods can be helpful. This can make viewing all data more feasible in some situations.

df_transposed = df.T
print(df_transposed.to_string())

Remember, this technique should be used with caution as transposing very large DataFrames can be computationally expensive.

Using External Tools

When all else fails, or when you’re looking for a more interactive and visual way to explore your DataFrame, external tools like Jupyter Notebooks or Google Colab can offer more flexibility. These platforms automatically adjust to display large DataFrames more conveniently and interactively.

Conclusion

In this tutorial, we’ve covered several methods to print all columns of a huge DataFrame in Pandas, ranging from simple changes in display settings to leveraging external tools for an enhanced viewing experience. Remember, the method you choose will depend on your specific needs and the resources available to you. Adopting these strategies will undoubtedly make it easier to inspect and understand your large datasets.

Next Article: Pandas: How to clear all rows in a DataFrame (keep column names)

Previous Article: Pandas: How to print a DataFrame without index (3 ways)

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)