When performing data analysis tasks in pandas, we might end up in a situation where not all the data is displayed. There could be too many rows of data, and not all of the content of each column is displayed due to the limit on the column’s maximum width; or, the float values may not be displayed with the precision that we want.
There are various options in pandas that you can use to overwrite these settings to accommodate your needs. In this shot, we will look at three of the most common settings that we can modify.
Often, we work with huge datasets that can contain many rows of data. While viewing this data in pandas, you might not be able to see all the rows (maybe you’ll see the top 30 rows and last 30 rows with a ...
inbetween). But what if we want to view all the data?
To do this, we need to change the default setting of the maximum number of rows to be displayed. Have a look at the code snippets below to understand this better.
import pandas as pddrinks = pd.read_csv('http://bit.ly/drinksbycountry')print(drinks[['country','beer_servings']])max_rows = pd.get_option('display.max_rows')print("Maximum rows that can be displayed: ", max_rows)
Explanation:
DisplayMaxRowsDefault
tab:
...
has been printed in between the data, and incomplete data has been shown.get_option()
function and pass the parameter as display.max_rows
to see, by default, how many rows can be displayed. We see that the number is , and so we will want to change this setting.DisplayMaxRowsCustom
tab:
display.max_rows
to None
to display all the rows in the data.reset_option()
to reset any setting back to its default value.While viewing the data, you might have observed that many columns do not print all the content in a certain cell. This is due to the maximum column width property. Let’s see how this setting can be changed.
import pandas as pdtrain = pd.read_csv('http://bit.ly/kaggletrain')print(train[['Name','Sex']])max_colwidth = pd.get_option('display.max_colwidth')print("Maximum column width is: ", max_colwidth)
Explanation:
DisplayMaxColWidthDefault
tab:
display.max_colwidth
).Name
is not displayed completely.DisplayMaxColWidthCustom
tab:
display.max_colwidth
to 1000.Many times, there are float values in our data that we will want to display two or three digits after the decimal point. Take a look at the code snippet below to see how this problem can be solved.
import pandas as pdtrain = pd.read_csv('http://bit.ly/kaggletrain')print(train[['Name','Sex', 'Fare']])max_precision = pd.get_option('display.precision')print("Maximum precision is: ", max_precision)
Explanation:
DisplayMaxPrecisionDefault
tab:
Fare
, that contains the float values.high
(there are many numbers after the decimal); so, we will need to change the default precision.DisplayMaxPrecisionCustom
tab:
display.precision
to 2, meaning it will only print 2 digits after the decimal point.In this way, you can change the default display settings in pandas as per your needs.