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.
Topics Covered
display dataframe in proper format
Let us create a dataframe to display dataframe in proper format. The dataframe is as follows:
trainno | trainname | arrival | departure | source | destination | fare |
14707 | Ranakpur express | 15:20 | 15:22 | Dadar | Bikaner | 1225 |
22954 | Gujarat Superfast Express | 08:00 | 08:05 | Mumbai Central | Ahmedabad | 990 |
20907 | Sayaji Nagri Express | 07:00 | 22:25 | Bhuj | Dadar | 1570 |
19131 | Kutch Superfast Express | 09:00 | 20:15 | Bhuj | Mumbai Central | 1600 |
22960 | Vadnagar Intercity Express | 00:35 | 05:45 | Valsad | Vadnagar | 605 |
19033 | Gujarat Queen | 00:50 | 04:00 | Valsad | Ahmedabad | 575 |
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:
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 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
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:
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)
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: