pandas print entire DataFrame | print as table, grid, text, html, psql, github, tsv format
In this pandas tutorial, we will discuss several ways to:
-
print pandas dataframe,
-
print pandas dataframe as table (psql format),
-
print pandas dataframe with grid,
-
print pandas dataframe as text,
-
print pandas dataframe as restructured text format,
-
print pandas dataframe as html table,
-
print pandas dataframe as github format,
-
print pandas dataframe as tsv format,
Here we will understand how to print pandas dataframe in different format. But before that lets create a dataframe first.
Pandas DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.
Example:
In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output:
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
If we want to display the dataframe in several ways, we have to use tabulate module.
This module supports several ways to print pandas DataFrame. So, before that we need to install this package.
Syntax:
pip install tabulate
DataFrame will be displayed in psql format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='psql')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will display the dataframe in psql format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='psql'))
Output: Given below we print pandas dataframe as table
+-------+--------------+-------------------+-------------------+---------------+
| | college_id | college_name | college_address | Total Staff |
|-------+--------------+-------------------+-------------------+---------------|
| one | c-001 | vignan university | guntur | 1200 |
| two | c-021 | vvit | guntur | 3422 |
| three | c-002 | RVR - JC | guntur | 5644 |
| four | c-004 | Andhra University | guntur | 670 |
+-------+--------------+-------------------+-------------------+---------------+
DataFrame will be displayed in fancy grid format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='fancy_grid')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this print pandas dataframe with grid example, we will display the dataframe in fancy_grid format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='fancy_grid'))
Output: print pandas dataframe with grid result
╒═══════╤══════════════╤═══════════════════╤═══════════════════╤═══════════════╕
│ │ college_id │ college_name │ college_address │ Total Staff │
╞═══════╪══════════════╪═══════════════════╪═══════════════════╪═══════════════╡
│ one │ c-001 │ vignan university │ guntur │ 1200 │
├───────┼──────────────┼───────────────────┼───────────────────┼───────────────┤
│ two │ c-021 │ vvit │ guntur │ 3422 │
├───────┼──────────────┼───────────────────┼───────────────────┼───────────────┤
│ three │ c-002 │ RVR - JC │ guntur │ 5644 │
├───────┼──────────────┼───────────────────┼───────────────────┼───────────────┤
│ four │ c-004 │ Andhra University │ guntur │ 670 │
╘═══════╧══════════════╧═══════════════════╧═══════════════════╧═══════════════╛
DataFrame will be displayed in plain text format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='plain')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will print pandas dataframe as text or display the dataframe in plain-text format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='plain'))
Output: print pandas dataframe as text result is given below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
DataFrame will be displayed in restructured text format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='rst')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will display the dataframe in restructured text format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='rst'))
Output:
===== ============ ================= ================= =============
.. college_id college_name college_address Total Staff
===== ============ ================= ================= =============
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
===== ============ ================= ================= =============
DataFrame will be displayed in Hyper Text Markup language format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='html')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will display or print pandas dataframe as html table.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='html'))
Output: print pandas dataframe as html table result is given below
<table>
<thead>
<tr><th> </th><th>college_id </th><th>college_name </th><th>college_address </th><th style="text-align: right;"> Total Staff</th></tr>
</thead>
<tbody>
<tr><td>one </td><td>c-001 </td><td>vignan university</td><td>guntur </td><td style="text-align: right;"> 1200</td></tr>
<tr><td>two </td><td>c-021 </td><td>vvit </td><td>guntur </td><td style="text-align: right;"> 3422</td></tr>
<tr><td>three</td><td>c-002 </td><td>RVR - JC </td><td>guntur </td><td style="text-align: right;"> 5644</td></tr>
<tr><td>four </td><td>c-004 </td><td>Andhra University</td><td>guntur </td><td style="text-align: right;"> 670</td></tr>
</tbody>
</table>
DataFrame will be displayed in github format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='github')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will display the dataframe in github format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='github'))
Output:
| | college_id | college_name | college_address | Total Staff |
|-------|--------------|-------------------|-------------------|---------------|
| one | c-001 | vignan university | guntur | 1200 |
| two | c-021 | vvit | guntur | 3422 |
| three | c-002 | RVR - JC | guntur | 5644 |
| four | c-004 | Andhra University | guntur | 670 |
DataFrame will be displayed in tab separated value format.
Syntax:
tabulate(dataframe, headers='keys', tablefmt='tsv')
where.
1. dataframe is the input dataframe
2. headers refers the column names for the given format
3. tablefmt refers the dataframe format to be returned.
Example:
In this example, we will display the dataframe in tsv format.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# display the dataframe
print(tabulate(data, headers='keys', tablefmt='tsv'))
Output:
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 16,2022