13 Configuring Pandas
This chapter covers:
- Configuring pandas display settings for both the Notebook and a single cell
- Limiting the number of printed
DataFrame
rows and columns - Altering the precision of decimal-point numbers
- Truncating text content in a
DataFrame
cell - Rounding numeric values when they fall below a floor
As we've worked through the book's datasets, we've seen how pandas improves our user experience by making sensible decisions on data presentation. For example, when we output a 1000-row DataFrame
, the library assumes we'd prefer to see 30 rows from the beginning and 30 rows from the end rather than the whole dataset, which can clutter up the screen. We can customize pandas' display settings to fit our own project's needs. The configurable options we can set include row and column limits, floating-point precision, value rounding, and more. Now that we're familiar with many of pandas' default presentation preferences let's get our hands dirty and see how we can switch things up.
13.1 Getting and Setting pandas Options
Let's begin by importing the pandas library and assigning it an alias of pd
.
In [1] import pandas as pd
This chapter's dataset, happiness.csv
, is a ranking of the world's nations by happiness. The polling form Gallup gathers the data with support from the United Nations. Each row includes a nation's aggregate score alongside individual scores for GDP per capita, social support, life expectancy, and generosity. The dataset holds 6 columns and 156 rows.