Python show all dataframe

How to print entire DataFrame in 10 different formats [Practical Examples]

Different methods to display entire DataFrame in pandas

In this tutorial we will discuss how to display the entire DataFrame in Pandas using the following methods:

  • Using set_option() method
  • Display with or without index
  • Display in Markdown format
  • Display in psql format
  • Display in plain-text format
  • Display in RST format
  • Display in github format
  • Display in pretty format
  • Display in tsv format
  • Display in HTML format

Create pandas DataFrame with example data

DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.

We can create the DataFrame by using pandas.DataFrame() method.

pandas.DataFrame(input_data,columns,index)

It will take mainly three parameters

  1. input_data is represents a list of data
  2. columns represent the columns names for the data
  3. index represent the row numbers/values
Читайте также:  Python read file line by line with open

We can also create a DataFrame using dictionary by skipping columns and indices.

Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.

# import the module import pandas # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # display the dataframe print(dataframe)
 id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2

1. Print entire DataFrame using set_option() method

Here we are going to display the entire dataframe

with this method, we can display n number of rows and columns.

# display all the rows pandas.set_option('display.max_rows', None) # display all the columns pandas.set_option('display.max_columns', None) # set width - 100 pandas.set_option('display.width', 100) # set column header - left pandas.set_option('display.colheader_justify', 'left') # set precision - 5 pandas.set_option('display.precision', 5)
# import the module import pandas # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # display all the rows pandas.set_option('display.max_rows', None) # display all the columns pandas.set_option('display.max_columns', None) # set width - 100 pandas.set_option('display.width', 100) # set column header - left pandas.set_option('display.colheader_justify', 'left') # set precision - 5 pandas.set_option('display.precision', 5) # display the dataframe print(dataframe) 
 id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2

2. Print entire DataFrame with or without index

Here we are going to display the entire dataframe.

With this method, we can display n number of rows and columns with and with out index .

dataframe.to_string(index=False) dataframe.to_string(index=True) 
# import the module import pandas # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # display the dataframe with out index print("Pandas DataFrame without index:\n", dataframe.to_string(index=False)) # display the dataframe with index print("Pandas DataFrame with index:\n", dataframe.to_string(index=True)) 
Pandas DataFrame without index: id name cost quantity foo-23 ground-nut oil 567.00 1 foo-13 almonds 562.56 2 foo-02 flour 67.00 3 foo-31 cereals 76.09 2 Pandas DataFrame with index: id name cost quantity item-1 foo-23 ground-nut oil 567.00 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67.00 3 item-4 foo-31 cereals 76.09 2

3. Print entire DataFrame in Markdown format

Here we are going to display in markdown format

# import the module import pandas # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay in mark down print(dataframe.to_markdown()) 
| | id | name | cost | quantity | |:-------|:-------|:---------------|-------:|-----------:| | item-1 | foo-23 | ground-nut oil | 567 | 1 | | item-2 | foo-13 | almonds | 562.56 | 2 | | item-3 | foo-02 | flour | 67 | 3 | | item-4 | foo-31 | cereals | 76.09 | 2 |

4. Print entire DataFrame in psql format

Here we are going to display the entire dataframe in psql format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='psql')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in psql format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in psql format print(tabulate(dataframe, headers='keys', tablefmt='psql')) 
+--------+--------+----------------+--------+------------+ | | id | name | cost | quantity | |--------+--------+----------------+--------+------------| | item-1 | foo-23 | ground-nut oil | 567 | 1 | | item-2 | foo-13 | almonds | 562.56 | 2 | | item-3 | foo-02 | flour | 67 | 3 | | item-4 | foo-31 | cereals | 76.09 | 2 | +--------+--------+----------------+--------+------------+

5. Print entire DataFrame in plain-text format

Here we are going to display the entire dataframe in plain-text format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='plain-text')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in plain-text format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in plain-text format print(tabulate(dataframe, headers='keys', tablefmt='plain-text')) 
 id name cost quantity ------ ------ -------------- ------ ---------- item-1 foo-23 ground-nut oil 567 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67 3 item-4 foo-31 cereals 76.09 2

6. Print entire DataFrame in RST format

Here we are going to display the entire dataframe in RST format. RST stands for restructured text .

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='RST')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in RST format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in RST format print(tabulate(dataframe, headers='keys', tablefmt='RST')) 
 id name cost quantity ------ ------ -------------- ------ ---------- item-1 foo-23 ground-nut oil 567 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67 3 item-4 foo-31 cereals 76.09 2

7. Print entire DataFrame in github format

Here we are going to display the entire dataframe in github format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='github ')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in github format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in github format print(tabulate(dataframe, headers='keys', tablefmt='github')) 
| | id | name | cost | quantity | |--------|--------|----------------|--------|------------| | item-1 | foo-23 | ground-nut oil | 567 | 1 | | item-2 | foo-13 | almonds | 562.56 | 2 | | item-3 | foo-02 | flour | 67 | 3 | | item-4 | foo-31 | cereals | 76.09 | 2 |

8. Print entire DataFrame in pretty format

Here we are going to display the entire dataframe in pretty format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='pretty')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in pretty format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in pretty format print(tabulate(dataframe, headers='keys', tablefmt='pretty')) 
+--------+--------+----------------+--------+----------+ | | id | name | cost | quantity | +--------+--------+----------------+--------+----------+ | item-1 | foo-23 | ground-nut oil | 567.0 | 1 | | item-2 | foo-13 | almonds | 562.56 | 2 | | item-3 | foo-02 | flour | 67.0 | 3 | | item-4 | foo-31 | cereals | 76.09 | 2 | +--------+--------+----------------+--------+----------+

9. Print entire DataFrame in tsv format

Here we are going to display the entire dataframe in tab separated value format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='tab')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in tab format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in tab format print(tabulate(dataframe, headers='keys', tablefmt='tab')) 
 id name cost quantity ------ ------ -------------- ------ ---------- item-1 foo-23 ground-nut oil 567 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67 3 item-4 foo-31 cereals 76.09 2

10. Print entire DataFrame in HTML format

Here we are going to display the entire dataframe in HTML (Hyper text markup language) format.

This is a format available in tabulate package. so we need to install this package.

Install tabulate using pip:

Syntax for this format:

tabulate(dataframe, headers='keys', tablefmt='HTML')

where, dataframe is the input dataframe

Example: Python program to display the entire dataframe in HTML format

# import the module import pandas from tabulate import tabulate # consider the food data food_input= <'id':['foo-23','foo-13','foo-02','foo-31'], 'name':['ground-nut oil','almonds','flour','cereals'], 'cost':[567.00,562.56,67.00,76.09], 'quantity':[1,2,3,2]># pass this food to the dataframe by specifying rows dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4']) # dispay the entire dataframe in HTML format print(tabulate(dataframe, headers='keys', tablefmt='HTML')) 
 id name cost quantity ------ ------ -------------- ------ ---------- item-1 foo-23 ground-nut oil 567 1 item-2 foo-13 almonds 562.56 2 item-3 foo-02 flour 67 3 item-4 foo-31 cereals 76.09 2

Summary

In this article we discussed how to print entire dataframe in following formats:

  • Markdown format
  • psql format
  • plain-text format
  • RST format
  • github format
  • pretty format
  • tsv format
  • HTML format

References

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью