Comprehensive notes on display dataframe in proper format for Class 12 Informatics Practices

This article will help all Pandas learners to display dataframe in proper format. Before reading this article you should know how to create a dataframe.

If you are a beginner to the data frame, I recommend you to follow this article and learn how to create a dataframe.

Creating a pandas dataframe

display dataframe in proper format

Let us create a dataframe to display dataframe in proper format. The dataframe is as follows:

trainnotrainnamearrivaldeparturesourcedestinationfare
14707Ranakpur express15:20 15:22DadarBikaner1225
22954Gujarat Superfast Express08:0008:05Mumbai CentralAhmedabad990
20907Sayaji Nagri Express07:0022:25BhujDadar1570
19131Kutch Superfast Express09:0020:15BhujMumbai Central1600
22960Vadnagar Intercity Express00:3505:45ValsadVadnagar605
19033Gujarat Queen00:5004:00ValsadAhmedabad575

When dataframe is prepapred in Python Pandas, It will adjust the row height and column width by default. It will also adjust the maximum rows and maximum columns and then if more rows and columns are there it will be truncated. Have a look at this.

Code:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
print(df)

Output:

display dataframe in proper format

Observe the output attached here. As you can see the dataframe is printed with all possible rows and columns but third column onwards you can see … which truncated columns. Let us adjust this first and display all the columns.

display dataframe in proper format by adjusting max_columns

To adjust the truncated dataframe, python provides pd.set_option() function. This function require a parameter – ‘display.max_columns’ along with None to display all columns. Observe this code:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
pd.set_option('display.max_columns', None)
print(df)
display dataframe in proper format

display dataframe in proper format by adjusting max_rows

As similar as that if rows are more they will be truncated. Hence pd.set_options() function with a parameter display.max_rows along with None is used. If a number is specified with display_maxrows() parameter, it will display those number of rows.

displaying limited rows

In the following I have set 4 rows maximum. Observe this:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
pd.set_option('display.max_rows', 4)
print(df)

Output

display dataframe in proper format

To display all rows specify None parameter along with display.max_rows. Have a look it:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
pd.set_option('display.max_rows', None)
print(df)

Output:

display all rows and all columnd of dataframe

Setting width of dataframe – display dataframe in proper format

To set width of dataframe in output, use display.width parameter with pd.set_option() function. Specify any numeric value for dataframe width. Observe this code:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width',150)
print(df)
setting width of dataframe in pandas

I have set 150 width to cover entire horizontal space for the dataframe. It will display remaining columns into next line if entire horizontal space is not covered. Here it is:

import pandas as pd
d={'TrainNo':[14707,22954,20907,19131,22960,19033],
   'TrainName':['Ranakpur Express','Gujarat Superfast Express','Sayajingari Express',
              'Kutch Superfase Express','Vadnagar Intercity','Gujarat Queen'],
   'Arrival':['15:20','08:00','07:00','09:00','00:35','00:50'],
   'Departure':['15:22','08:05','22:25','20:15','05:45','04:00'],
   'Source':['Dadar','Mumbai Central','Bhuj','Bhuj','Valsad','Valsad'],
   'Destination':['Bikaner','Ahmedabad','Dadar','Mumbai Central','Vadnagar','Ahmedabad'],
   'Fare':[1225,990,1570,1600,605,575]
  }
df=pd.DataFrame(d)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width',80)
print(df)

Here the width is 80, hence the output wil be:

setting witdh of DF

Leave a Reply