DataFrame

pandas · Essentials · 12 min

Nearly all pandas work means building, inspecting and transforming DataFrames.

A DataFrame is a two-dimensional table: a collection of columns, each a Series with its own dtype, all sharing the same row index. Column labels live in df.columns.

The most common way to build one by hand is from a dict of lists (one key per column) or a list of dicts (one per row):

df = pd.DataFrame({"city": ["Oslo", "Lima"], "temp": [3.5, 19.0]})

First things to do with any DataFrame:

  • df.shape gives (rows, columns); len(df) gives the row count
  • df.head() and df.tail() show the first or last rows; df.sample(5) a random few
  • df.dtypes lists each column's type; df.info() adds non-null counts and memory use
  • df.columns and df.index show the labels

Selecting one column with df["temp"] returns a Series; a list of columns, df[["city", "temp"]], returns a DataFrame. Attribute access (df.temp) also works for simple names, but breaks for names with spaces or that collide with methods (df.count is a method), so square brackets are the safer habit.

Because columns are stored separately and can have different types, think of a DataFrame as column-oriented. Operations on whole columns are fast; looping over rows is slow and rarely necessary.

The row index defaults to 0..n-1 (a memory-free RangeIndex). You can promote a column to be the index with df.set_index("city"), which makes label lookups by city possible, and undo it with reset_index(). Most methods return a new DataFrame rather than modifying the original, so assign the result: df = df.set_index("city").

Example

import pandas as pd

df = pd.DataFrame({
    "city": ["Oslo", "Lima", "Cairo", "Perth"],
    "temp": [3.5, 19.0, 28.2, 22.4],
    "rain_mm": [49, 1, 0, 14],
})
print(df.shape)                # (4, 3)
print(df.head(2))
#    city  temp  rain_mm
# 0  Oslo   3.5       49
# 1  Lima  19.0        1

print(df["temp"].max())        # 28.2   a column is a Series
print(df[["city", "rain_mm"]].columns.tolist())   # ['city', 'rain_mm']
print(df.dtypes["rain_mm"])    # int64

rows = [{"city": "Rome", "temp": 15.1, "rain_mm": 70}]
print(pd.DataFrame(rows))

by_city = df.set_index("city")
print(by_city.loc["Lima", "temp"])   # 19.0

Remember

  • A DataFrame is a set of Series columns sharing one row index
  • Start with shape, head, dtypes and info
  • df['col'] gives a Series, df[['a', 'b']] a DataFrame
  • Most methods return a new object; assign the result

Watch out for

  • Calling df.set_index('id') and expecting df to change: it returns a new DataFrame.

Check yourself

  1. What type does `df[['price']]` return?
  2. Why is `df['count']` safer than `df.count` for a column named 'count'?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: pandas.pydata.org/docs/user_guide/dsintro.html#dataframe