How to print an entire Pandas DataFrame in Python?
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:
There are 4 methods to Print the entire pandas Dataframe:
- Use to_string() Method
- Use pd.option_context() Method
- Use pd.set_options() Method
- Use pd.to_markdown() Method
Method 1: Using to_string()
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)
Method 2 : Using pd.option_context()
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)
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.
Method 3 : Using pd.set_option()
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)
Method 4 : Using to_markdown()
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)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course