Pandas Print DataFrame Methods: Easy Export & Display

If you've been working with pandas, the powerful data manipulation library in Python, you'll often find the need to display your DataFrame in various formats. One of the most common questions asked is, "How do I print out a pandas DataFrame?"

Pandas Cheat Sheet for Beginners πŸΌπŸ“Š
Pandas Cheat Sheet for Beginners πŸΌπŸ“Š

Pandas provides several methods to print or display a DataFrame, each serving a different purpose. Let's explore these methods and see how they can help you understand and present your data effectively.

Pandas DataFrames overview in Python
Pandas DataFrames overview in Python

The most straightforward way to print a DataFrame is by using the built-in Python function, print() or pandas' own display() function. Both will display the first 5 rows and the last 5 rows of your DataFrame by default, giving you a quick overview of your data.

Here's how you can use them:

Pandas Cheat Sheet 🐼 | Complete Python Pandas Guide for Data Analysis
Pandas Cheat Sheet 🐼 | Complete Python Pandas Guide for Data Analysis
  • print(df): This will print a summary of your DataFrame.
  • display(df): This will display the full DataFrame in a more readable format, depending on your Jupyter notebook or other interactive environment.

Exploring Rows and Columns: head() and tail() Functions

Python Pandas DataFrame: load, edit, view data
Python Pandas DataFrame: load, edit, view data

When you want to see the first or last 'n' rows of your DataFrame, pandas offers two specialized functions: head() and tail().

What's the difference between print() and these functions? While print() and display() show a summary, head() and tail() display the actual data from the first and last rows, respectively. Here's how to use them:

  • df.head(n): Replace 'n' with the number of rows you want to see from the top.
  • df.tail(n): Similarly, replace 'n' for rows from the bottom.
How to Print Pandas DataFrame without Index
How to Print Pandas DataFrame without Index

Formatting Your Output: to_string() and to_html() Functions

Sometimes, you might want to print your DataFrame in a specific format, like a plain string (for saving to a file) or HTML (for displaying in a web environment). Pandas provides to_string() and to_html() functions for these purposes.

Let's see how to use them:

Sort a Pandas DataFrame
Sort a Pandas DataFrame
  • df.to_string(path, na_rep='NaN'): This will write the DataFrame to a file at the given 'path', replacing missing values with 'NaN'.
  • df.to_html(path_or_buf=None, reparations=True): This will write the DataFrame as an HTML table to the given 'path_or_buf'. If no path is given, the HTML is returned as a string.

Creating Tables in Jupyter Notebooks: display() with 'max_rows' and 'max_cols' Parameters

the text pandas dataframe is displayed in black and red letters on a white background
the text pandas dataframe is displayed in black and red letters on a white background
🐼 Welcome to the Complete Pandas Full Course! πŸ“Š Master Pandas from Beginner....
🐼 Welcome to the Complete Pandas Full Course! πŸ“Š Master Pandas from Beginner....
Pandas Cheat Sheet | Complete Data Analysis Guide with Python
Pandas Cheat Sheet | Complete Data Analysis Guide with Python
the pandas worksheet is filled with information about data and other important things
the pandas worksheet is filled with information about data and other important things
23 Efficient Ways of Subsetting a Pandas DataFrame | Towards Data Science
23 Efficient Ways of Subsetting a Pandas DataFrame | Towards Data Science
a table with the words important pandas and other symbols
a table with the words important pandas and other symbols
Pandas For 3 Part.... 🐼
Pandas For 3 Part.... 🐼

In Jupyter notebooks, you can control the number of rows and columns displayed when using the display() function. This is particularly useful when working with large DataFrames that you don't want to scroll through.

Here's how to do it:

  • display(df, max_rows=n, max_cols=m): Replace 'n' and 'm' with the desired number of rows and columns to display.

Lastly, remember that pandas is designed for efficiency, so printing large DataFrames can be resource-intensive. Always consider the size of your data and choose the most appropriate method for your needs.

Happy data manipulation!