ADVERTISEMENT
ADVERTISEMENT

How to pretty-print an entire Pandas DataFrame?

Given a large DataFrame, we need to print the entire DataFrame.
Submitted by Pranit Sharma, on April 17, 2022

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?

Pandas have the set_options() method which allows us to set different properties according to our requirements and pretty print the entire Dataframe. Below are some 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.

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand with the help of an example.

Example:

# 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("Created DataFrame:\n")
print(df,"\n\n")

Output:

Created DataFrame:

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

[13 rows x 8 columns] 
pretty-print an entire Pandas DataFrame | Output

Hence, by setting different options using the set_options() method, we are now able to see all the columns of our DataFrame including 'Profession' and 'Title'.

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.