Paul's Python CoursePPC

pandas DataFrames and Series

Not met yet

pd.read_csv("census.csv") reads a CSV file into a DataFrame: rows, and named columns that each hold one type. census.shape, census.head() and census.dtypes tell you what you've got. census["age"] is one column, a Series, with methods like mean(), max() and value_counts(). sort_values("age") gives back a sorted copy and leaves the table alone.

Try it

main.py
import pandas as pd

people = pd.DataFrame({
    "name": ["Edith Hart", "Walter Lee", "Mary Lee"],
    "age": [34, 88, 52],
})
print(people.shape)
print(people["age"].mean())
print(people.sort_values("age", ascending=False).head(1))

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks one column of a DataFrame is a plain Python list, rather than a Series with its own index and methods.
  • Thinks sort_values() reorders the table itself, rather than handing back a sorted copy that has to be kept.

Where it comes up