dtypes in pandas
What each column is holding, and how to change it on purpose.
Ask before you calculate
dtypes tells you what every column is. A column of numbers read from a file is very often object, which means text, and averaging it will either fail or lie.
import pandas as pd
df = pd.DataFrame({"score": ["92", "88"], "n": [1, 2]})
print(df.dtypes)
Converting a column
astype changes a column's type when every value can make the trip. to_numeric with errors="coerce" is the forgiving version: anything it cannot convert becomes missing rather than raising.
import pandas as pd
df = pd.DataFrame({"score": ["92", "88"]})
df["score"] = df["score"].astype(int)
print(df["score"].sum())
messy = pd.Series(["92", "n/a"])
print(pd.to_numeric(messy, errors="coerce").tolist())
Exercise
Try It YourselfWrite numeric_total(df) that returns the sum of the score column, where the scores arrived as text and anything that is not a number counts as zero.
Press Run to see output