CSCI 127 Joy and Beauty of Data · Unit 4: Data science · on Final exam (Exam 3)
4.3 pandas DataFrames
Study notes for pandas DataFrames in CSCI 127 Joy and Beauty of Data at Montana State University: the big ideas, code you should know, a worked example, common mistakes and an exam tip. The same topic has endless practice questions and flashcards in Mathub.
Big ideas
import pandas as pd. A DataFrame is a table with named columns; a Series is one column.pd.read_csv("file.csv")loads data;pd.DataFrame({...})builds one from lists.- First look:
df.head()(5 rows),df.shape,df.columns,df.info(),df.describe(). - Select:
df["col"]a column,df[["a", "b"]]several,df.loc[row_label, "col"]by label,df.iloc[0]by position. Filter with a mask:df[df["score"] > 80]. - Compute:
df["col"].mean(),.max(),.value_counts(),df.sort_values("col", ascending=False), new columnsdf["pct"] = df["score"] / 100. - Group:
df.groupby("major")["score"].mean()answers “average per group”.df.plot(...)hands the table to matplotlib.
Code you should know
Build, inspect, filter, sort
import pandas as pd
df = pd.DataFrame({
"name": ["Ana", "Ben", "Cy", "Dee"],
"major": ["CS", "Math", "CS", "Bio"],
"score": [91, 84, 77, 95]
})
print(df.shape, list(df.columns))
print(df.head(2))
print(df["score"].mean(), df["score"].max())
print(df[df["score"] > 80]["name"].tolist())
print(df.sort_values("score", ascending=False).iloc[0]["name"])
print(df["major"].value_counts())Output
(4, 3) ['name', 'major', 'score'] name major score 0 Ana CS 91 1 Ben Math 84 86.75 95 ['Ana', 'Ben', 'Dee'] Dee major CS 2 Math 1 Bio 1
groupby and a new column
import pandas as pd
df = pd.DataFrame({"city": ["Bozeman", "Bozeman", "Billings", "Billings"], "day": ["Mon", "Tue", "Mon", "Tue"], "high": [61, 68, 70, 74]})
df["high_c"] = (df["high"] - 32) * 5 / 9
print(df.groupby("city")["high"].mean())
print(df.groupby("city")["high_c"].max().round(1))
print(df.loc[df["high"].idxmax(), "city"])Output
city Billings 72.0 Bozeman 64.5 Name: high, dtype: float64 city Billings 23.3 Bozeman 20.0 Name: high_c, dtype: float64 Billings
Worked example
Given a DataFrame
df with columns name, major, score, write one line for each: the average score, the rows with score above 85, the number of students per major.Show the solution
df["score"].mean(); df[df["score"] > 85]; df["major"].value_counts() (or df.groupby("major").size()).Common mistakes
df.score > 85gives a mask of True/False; you still needdf[mask]to get rows.- Confusing
loc(labels) withiloc(positions). - Forgetting that most methods return a new DataFrame; assign the result.
Exam tipKnow the six verbs: read, look (head/shape), select, filter, summarize (mean/value_counts), group.
Keep going in Mathub
Mathub is free for Montana State students: an endless quizzer with worked explanations, one-question lessons, flashcards, exam prep checklists, the class calendar with every deadline, a grade calculator and a study board for CSCI 127.