Python Programming · High School (11–12) · 2 courses, 20 worked problems, 30 practice questions
The pandas library works with tables of data. Its main object is the DataFrame, which has named columns and numbered rows, like a spreadsheet. You can read a CSV file with pd.read_csv(), look at the first rows with .head() and pick a column by its name. You can filter rows, such as all planets heavier than the Earth, and sort them. Methods such as .mean(), .describe() and .groupby() summarise the data in a line or two, and two tables can be merged on a shared column. Scientists use pandas to clean and study results.
.mean(), .describe() and .groupby()Choose an answer, then check it. Each one shows a full explanation.
Three elements of period 5 are in a table. What does this print? import pandas as pd; df = pd.DataFrame({"sym": ["Sr", "Ba", "Cd"], "ie": [549, 503, 868]}); print(df["ie"].max())
The same table is sorted. What does this print? import pandas as pd; df = pd.DataFrame({"sym": ["Sr", "Ba", "Cd"], "ie": [549, 503, 868]}); print(list(df.sort_values("ie")["sym"]))
A table has the columns sym and ie. What does df["IE"] do?
A filter is applied to the strontium table. What does this print? import pandas as pd; df = pd.DataFrame({"sym": ["Sr", "Ba", "Cd"], "ie": [549, 503, 868]}); print(len(df[df["ie"] > 540]))
A computed column compares each element with the lowest. What does this print? import pandas as pd; df = pd.DataFrame({"sym": ["Sr", "Ba", "Cd"], "ie": [549, 503, 868]}); df["ratio"] = df["ie"] / df["ie"].min(); print(round(df["ratio"].iloc[0], 2))
Which line keeps the s-block rows whose ie is above kJ/mol?
Which graph shows the first ionisation enthalpy of the first ten elements against Z?
Strontium, barium and iodine are grouped by block. What does this print? import pandas as pd; df = pd.DataFrame({"block": ["s", "s", "p"], "ie": [549, 503, 1008]}); print(df.groupby("block")["ie"].mean()["s"])
Rubidium, strontium, indium and tin, in order of Z. What does this print? import pandas as pd; s = pd.Series([403, 549, 558, 709]); print(list(s.diff().dropna().astype(int)))
Across the first 36 elements, what is the sign of the correlation between atomic radius and first ionisation enthalpy?
Each one has a hint, the key idea and a full worked solution.