Show DataFrame as table in iPython Notebook

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Show DataFrame as table in iPython Notebook

How to Display DataFrames as Tables in iPython Notebook

Are you using iPython Notebook and struggling to display DataFrames as tables? Don't worry, you're not alone! Many users face this issue where trying to display multiple DataFrames leads to unexpected results. But fear not, we've got you covered with some easy solutions and tricks to ensure your DataFrames are displayed beautifully as tables.

The Problem: Collapsed Tables

Let's start by understanding the problem. When you use the following code in iPython Notebook:

df

You get a beautiful table with cells, neatly displayed. However, if you try to display multiple DataFrames one after another, like this:

df1
df2

You'll notice that the first beautiful table doesn't appear. Instead, you'll see the output of both DataFrames printed separately. Even if you use the "print" statement, like this:

print df1
print df2

The tables are printed in a different format that spills columns over and makes the output very tall, making it less readable and user-friendly.

The Solution: Pandas and Display Options

To force iPython Notebook to display DataFrames as beautiful tables, you can use the display() function from the IPython.display module. Here's how you can do it:

from IPython.display import display

display(df1)
display(df2)

Using display() ensures that each DataFrame is displayed as a separate table, just like how it appeared when you printed them one after another without using the print statement. This solves the problem of collapsed tables and makes it easier to analyze and compare multiple DataFrames.

Bonus Tips: Customizing the Display

If you want to customize the display of your DataFrames further, you can make use of the set_option() function from the pandas module. For example, you can adjust the maximum number of columns or rows to be displayed by using:

import pandas as pd

# Set the maximum number of columns to 10
pd.set_option('display.max_columns', 10)

# Set the maximum number of rows to 100
pd.set_option('display.max_rows', 100)

Feel free to experiment with different options to suit your specific needs and requirements.

Conclusion

Displaying DataFrames as tables in iPython Notebook shouldn't be a hassle. By using the display() function from the IPython.display module, you can easily solve the problem of collapsed tables and ensure that each DataFrame is displayed beautifully. Additionally, by utilizing the set_option() function from the pandas module, you can further customize the display according to your preferences.

Next time you're working with DataFrames in iPython Notebook, give these solutions a try and enjoy the clarity and simplicity of well-organized tables. Happy coding! 😄

Have you encountered any other challenges while working with iPython Notebook? Share your experiences and let's find solutions together! 💡

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my