How to pretty-print an entire Pandas DataFrame?

In this tutorial, we will learn how to pretty-print an entire Pandas DataFrame with the help of example? By Pranit Sharma Last updated : April 12, 2023

Overview

In the real world, data is huge so is the dataset. While importing a dataset and converting it into DataFrame, the default printing method does not print the entire DataFrame. It compresses the rows and columns. In this article, we are going to learn how to pretty-print the entire DataFrame?

Pretty-print an entire Pandas DataFrame

To pretty-print format an entire Pandas DataFrame, we use Pandas options such as pd.options.display.max_columns, pd.options.display.max_rows, and pd.options.display.width to set and customize global behaviour related to display/printing the DataFrame.

Pretty-print Options

The following are some of the pretty-print options:

  • display.max_columns: It defines the total number of columns to be printed. If None is passed as an argument, all columns would be printed.
  • display.max_rows: It defines the total number of rows that need to be printed. If None is passed as an argument all rows would be printed.
  • display.width: It is also an important option that defines the width of the display. If set to None, pandas will correctly auto-detect the width.

Let us understand with the help of an example.

Example 1: Print a Pandas DataFrame (Default Format)

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    "Name":['Hari','Mohan','Neeti','Shaily','Ram','Umesh','Shirish','Rashmi','Pradeep','Neelam','Jitendra','Manoj','Rishi'],
    "Age":[25,36,26,21,30,33,35,40,39,45,42,39,48],
    "Gender":['Male','Male','Female','Female','Male','Male','Male','Female','Male','Female','Male','Male','Male'],
    "Profession":['Doctor','Teacher','Singer','Student','Engineer','CA','Cricketer','Teacher','Teacher','Politician','Doctor','Manager','Clerk'],
    "Title":['Mr','Mr','Ms','Ms','Mr','Mr','Mr','Ms','Mr','Ms','Mr','Mr','Mr'],
    "Salary":[200000,50000,500000,0,100000,75000,10000000,50000,50000,200000,200000,150000,15000],
    "Location":['Amritsar','Indore','Mumbai','Bhopal','Gurugram','Pune','Banglore','Ranchi','Surat','Chennai','Shimla','Kolkata','Raipur'],
    "Marriage Status":[0,1,1,0,1,0,0,1,1,1,0,1,0]
}

# Now, Create DataFrame
df=pd.DataFrame(d)

# Printing the created DataFrame
print("DataFrame:\n")
print(df)

Output

DataFrame:

        Name  ...  Marriage Status
0       Hari  ...                0
1      Mohan  ...                1
2      Neeti  ...                1
3     Shaily  ...                0
4        Ram  ...                1
5      Umesh  ...                0
6    Shirish  ...                0
7     Rashmi  ...                1
8    Pradeep  ...                1
9     Neelam  ...                1
10  Jitendra  ...                0
11     Manoj  ...                1
12     Rishi  ...                0

[13 rows x 8 columns]

Example 2: Print a Pandas DataFrame in "Pretty" Format

In this example, we are setting the maximum rows and columns to display.

pd.options.display.max_rows = 5
pd.options.display.max_columns = 5

# Printing the  DataFrame
print("DataFrame:\n")
print(df)

Output:

DataFrame:

     Name  Age  ...  Location Marriage Status
0    Hari   25  ...  Amritsar               0
1   Mohan   36  ...    Indore               1
..    ...  ...  ...       ...             ...
11  Manoj   39  ...   Kolkata               1
12  Rishi   48  ...    Raipur               0

[13 rows x 8 columns]

Example 3: Print a Pandas DataFrame in "Pretty" Format (Display All Rows, Columns)

In this example, we are setting the maximum rows, columns, and width to display all rows and columns with all data.

pd.options.display.max_rows = 13
pd.options.display.max_columns = 8
pd.options.display.width = 1000

# Printing the  DataFrame
print("DataFrame:\n")
print(df)

Output:

DataFrame:

        Name  Age  Gender  Profession Title    Salary  Location  Marriage Status
0       Hari   25    Male      Doctor    Mr    200000  Amritsar                0
1      Mohan   36    Male     Teacher    Mr     50000    Indore                1
2      Neeti   26  Female      Singer    Ms    500000    Mumbai                1
3     Shaily   21  Female     Student    Ms         0    Bhopal                0
4        Ram   30    Male    Engineer    Mr    100000  Gurugram                1
5      Umesh   33    Male          CA    Mr     75000      Pune                0
6    Shirish   35    Male   Cricketer    Mr  10000000  Banglore                0
7     Rashmi   40  Female     Teacher    Ms     50000    Ranchi                1
8    Pradeep   39    Male     Teacher    Mr     50000     Surat                1
9     Neelam   45  Female  Politician    Ms    200000   Chennai                1
10  Jitendra   42    Male      Doctor    Mr    200000    Shimla                0
11     Manoj   39    Male     Manager    Mr    150000   Kolkata                1
12     Rishi   48    Male       Clerk    Mr     15000    Raipur                0

Python Pandas Programs »

All Python Pandas Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.