TExcelTAD
Home › pandas › Pandas Getting Started

Pandas Getting Started

Before you can use pandas you need it installed and imported. On this site it is already available, so every playground just works. On your own computer you install it once with a package manager.

Installing pandas

If you have Python and pip, open a terminal and run:

bash
pip install pandas

If you use the Anaconda or Miniconda distribution, pandas is usually included already; otherwise run conda install pandas. Installing pandas also installs NumPy, which it depends on. For plotting you will also want Matplotlib:

bash
pip install matplotlib

Tip: Work inside a virtual environment (python -m venv .venv) so each project has its own package versions.

Importing pandas

By a near-universal convention, pandas is imported under the short alias pd:

python · try it yourself
import pandas as pd

temperatures = pd.Series([18.5, 21.0, 19.8], name="celsius")
print(temperatures)
0    18.5
1    21.0
2    19.8
Name: celsius, dtype: float64

You could write import pandas and then pandas.Series(...), but every tutorial, answer and colleague you meet will use pd, so stick with it. NumPy is likewise imported as np.

Checking the version

pandas changes over time, and some features (and default behaviours) depend on the version. You can always ask which one you have:

python · try it yourself
import pandas as pd

print(pd.__version__)
major = int(pd.__version__.split(".")[0])
print("pandas 2 or newer?", major >= 2)
2.3.3
pandas 2 or newer? True

This course uses pandas 2.3. If you follow along on an older 1.x install, a few outputs may differ slightly.

The two core objects

Everything in pandas revolves around two data structures:

Object Shape Think of it as
Series one dimension a single column with labels
DataFrame two dimensions a table: several Series sharing one index
python · try it yourself
import pandas as pd

city = pd.Series(["Oslo", "Lima", "Cairo"])
table = pd.DataFrame({
    "city": ["Oslo", "Lima", "Cairo"],
    "population_m": [0.7, 10.1, 10.0],
})
print(type(city).__name__, city.shape)
print(type(table).__name__, table.shape)
print(table)
Series (3,)
DataFrame (3, 2)
    city  population_m
0   Oslo           0.7
1   Lima          10.1
2  Cairo          10.0

The shape attribute tells you the size: a Series of 3 values has shape (3,), while the table has 3 rows and 2 columns, (3, 2). The next two lessons explore each object in detail.

Display options

When a table is too big for the screen, pandas shortens the printout with .... You can control how much is shown with pd.set_option():

python · try it yourself
import pandas as pd

numbers = pd.DataFrame({"n": range(100), "square": [i * i for i in range(100)]})
pd.set_option("display.max_rows", 6)
print(numbers)
pd.reset_option("display.max_rows")   # back to the default (60)
     n  square
0    0       0
1    1       1
2    2       4
..  ..     ...
97  97    9409
98  98    9604
99  99    9801

[100 rows x 2 columns]

The most useful display options:

Option Meaning
display.max_rows rows shown before the middle is replaced by ...
display.max_columns columns shown before the middle is replaced by ...
display.width characters per line before wide tables wrap
display.precision decimal places shown for floats

Note: Options only change how data is displayed, never the data itself. Use pd.reset_option("all") to go back to the defaults.

Test yourself

What is the conventional way to import pandas?

  1. import pandas as pa
  2. import pandas as pd
  3. from pandas import *
  4. import pd

What is the shape of a DataFrame with 5 rows and 3 columns?

  1. (3, 5)
  2. (5, 3)
  3. (15,)
  4. 5x3

What does pd.set_option("display.max_rows", 6) change?

  1. It deletes all rows after the sixth
  2. Only how many rows are printed
  3. The number of rows read from a file
  4. Nothing; it is not a real option