How to print an entire Pandas DataFrame in Python?

03 Aug, 2021
Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it. When we use a print large number of a dataset then it truncates. In this article, we are going to see how to print the entire pandas Dataframe or Series without Truncation. 
By default, the complete data frame is not printed if the length exceeds the default length, the output is truncated as shown below: 
Python3
import numpy as npfrom sklearn.datasets import load_irisimport pandas as pd # Loading irirs datasetdata = load_iris()df = pd.DataFrame(data.data,                  columns = data.feature_names)display(df)
Output:
Use to_string() Method
Use pd.option_context() Method
Use pd.set_options() Method
Use pd.to_markdown() Method
While this method is simplest of all, it is not advisable for very huge datasets (in order of millions) because it converts the entire data frame into a string object but works very well for data frames for size in the order of thousands.
Syntax: DataFrame.to_string(buf=None, columns=None, col_space=None, header=True, index=True, na_rep=’NaN’, formatters=None, float_format=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal=’.’, line_width=None)
Code:
Python3
import numpy as npfrom sklearn.datasets import load_irisimport pandas as pd data = load_iris()df = pd.DataFrame(data.data,                  columns = data.feature_names) # Convert the whole dataframe as a string and displaydisplay(df.to_string())
Output:
Pandas allow changing settings via the option_context() method and set_option() methods. Both the methods are identical with one difference that later one changes the settings permanently and the former do it only within the context manager scope.
Syntax : pandas.option_context(*args)
Code:
Python3
import numpy as npfrom sklearn.datasets import load_irisimport pandas as pd data = load_iris()df = pd.DataFrame(data.data,                  columns = data.feature_names) # The scope of these changes made to# pandas settings are local to with statement.with pd.option_context('display.max_rows', None,                       'display.max_columns', None,                       'display.precision', 3,                       ):    print(df)
Output:
Explanation: 
The above code uses certain options parameters such as the ‘display.max_rows‘ its default value is 10 & if the data frame has more than 10 rows its truncates it, what we are doing is making its value to None i.e all the rows are displayed now. Similarly, ‘display.max_columns‘ has 10 as default, we are also setting it to None.
display. precision  = 3 indicates that after decimal point shows up to 3 values here all the values have 1 value and therefore it doesn’t affect this example.
This method is similar to pd.option_context() method and takes the same parameters as discussed for method 2, but unlike pd.option_context() its scope and effect is on the entire script i.e all the data frames settings are changed permanently 
To explicitly reset the value use pd.reset_option(‘all’) method has to be used to revert the changes.
Syntax : pandas.set_option(pat, value)
Code:
Python3
import numpy as npfrom sklearn.datasets import load_irisimport pandas as pd data = load_iris()df = pd.DataFrame(data.data,                  columns = data.feature_names) # Permanently changes the pandas settingspd.set_option('display.max_rows', None)pd.set_option('display.max_columns', None)pd.set_option('display.width', None)pd.set_option('display.max_colwidth', -1) # All dataframes hereafter reflect these changes.display(df) print('**RESET_OPTIONS**') # Resets the optionspd.reset_option('all')display(df)
Output:
This method is similar to the to_string() method as it also converts the data frame to a string object and also adds styling & formatting to it.
Syntax : DataFrame.to_markdown(buf=None, mode=’wt’, index=True,, **kwargs)
Code:
Python3
import numpy as npfrom sklearn.datasets import load_irisimport pandas as pd data = load_iris()df = pd.DataFrame(data.data,                  columns=data.feature_names) # Converts the dataframe into str object with formattingprint(df.to_markdown())
Output:
sagar0719kumar
Picked
Python pandas-dataFrame
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
Introduction To PYTHON
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list