Course 4.2Lesson 1 of 3
A pandas DataFrame turns the periodic table into data you can question. You build one for the first 36 elements from a block of text, then pick columns, look up an element, sort, filter with conditions joined by &, and add a computed column.
This lesson is free to read. Enroll free to keep your place and take the tests.
Try the code and the simulations beside the lesson.
A printed periodic table is good to look at, but you cannot ask it "which elements have a radius above 170 pm?" A DataFrame can answer at once. It is a table in Python, with named columns and numbered rows, from the library pandas.
In this lesson each row is one element. The columns are:
symbol, z (the atomic number), group, period and block;radius, the atomic radius in pm;ie, the first ionisation enthalpy in kJ/mol;en, the Pauling electronegativity.The noble gases have no radius or electronegativity in the table. pandas shows such a gap as NaN, "not a number".
The data is typed into the program as plain text, one element to a line, with spaces between the values:
read_csv normally reads a file. io.StringIO(data) makes the text behave like a file, so no download is needed. sep=" " says the values are split by spaces, and names gives the column names. A value written nan becomes a missing value.
Two quick looks tell you the table came out right: df.shape gives the number of rows and columns, and df.head() prints the first five rows.
df["ie"] is one column, a Series.df[["symbol", "ie"]] is a smaller table with two columns. Note the two pairs of square brackets: the inner pair is a list of names.df.set_index("symbol") makes the symbols the row labels. Then .loc["Na", "ie"] looks up one value by its labels.df.sort_values("radius") puts the smallest atoms first; add ascending=False for the largest first. The original table is not changed unless you store the result.
A filter keeps the rows where a condition is true:
df["radius"] > 170 gives a column of True and False, one for each row, and df[...] keeps the True rows. To join two conditions, write & for "and" or | for "or", with brackets around each condition:
Hydrogen sits in group 1, but it is not an alkali metal, so the second condition leaves it out.
A new column can be worked out from the others, for every row at once. One kJ/mol is about of an electron volt per atom, so:
There is no loop: pandas divides the whole column in one step.
The program builds the table of the first 36 elements. It prints its size, the three highest ionisation enthalpies, the alkali metals and period 3, and then adds the column in electron volts.
df["IE"] stops with KeyError: 'IE', because the column is called ie.and between two conditions. df[(df["group"] == 1) and (df["ie"] > 600)] stops with ValueError: The truth value of a Series is ambiguous. Use &.& is done before ==, and the same ValueError appears..loc["Kr"] on a table indexed by symbol works only if krypton is a row; otherwise it is KeyError: 'Kr'.sort_values to change the table. Store the result: df = df.sort_values("ie").pd.read_csv(io.StringIO(text), sep=" ", names=cols) turns typed text into a DataFrame; nan marks a missing value.df["col"] is a column, df[["a", "b"]] is a smaller table, and df.set_index("symbol").loc[row, col] is one value.df[condition], and join conditions with & or |, each in brackets.df["new"] = df["old"] / number.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
df.shape and df.head() tell you about a new table?df is still in order of Z afterwards. Why?df["ie_eV"] = df["ie"] / 96.49?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…