Reading a CSV into pandas

One line replaces the whole of unit 1, and then you check what you got.

read_csv

It reads the header, splits the rows, and guesses each column's type. What it cannot do is know which guesses were wrong, which is why the next line is always a look at the result.

import pandas as pd

df = pd.read_csv("scores.csv")
print(df.shape)
print(df.dtypes)

Check before you trust

head shows you the first rows and dtypes shows you what pandas decided. Two lines, and they catch most of what would otherwise go wrong later.

import pandas as pd

df = pd.read_csv("scores.csv")
print(df.head(2))
print(df["score"].mean())

Exercise

Try It Yourself

A file scores.csv is supplied when your work is checked, with name and score columns. Write average_score(path) that reads it and returns the mean score, rounded to two decimal places.

Press Run to see output

Check Your Understanding