Retrieving ionization energies from a pandas DataFrame

The file ionization-energies.csv, contains the ionization energies (in eV) of some of the elements of the periodic table:

Ionization Energies (eV) of the first few elements of the periodic table
Element, IE1, IE2, IE3, IE4, IE5
H, 13.59844
He, 24.58741, 54.41778
Li, 5.39172, 75.64018, 122.45429
Be, 9.3227, 18.21116, 153.89661, 217.71865
B, 8.29803, 25.15484, 37.93064, 259.37521, 340.22580
C, 11.26030, 24.38332, 47.8878, 64.4939, 392.087
N, 14.53414, 29.6013, 47.44924, 77.4735, 97.8902
O, 13.61806, 35.11730, 54.9355, 77.41353, 113.8990
F, 17.42282, 34.97082, 62.7084, 87.1398, 114.2428
Ne, 21.5646, 40.96328, 63.45, 97.12, 126.21
Na, 5.13908, 47.2864, 71.6200, 98.91, 138.40

These data can be read into a DataFrame as follows. Here, we suppose that we are only interested in the first two periods of the periodic table and the first four ionization energies:

In [x]: df = pd.read_csv('ionization-energies.csv', skiprows=1, index_col=0,
   ...:                  usecols=range(5), nrows=11)
In [x]: df.columns = df.columns.str.strip()
In [x]: print('Second ionization energy of Li: {} eV'.format(df.loc['Li'].IE2))

Second ionization energy of Li: 75.64018 eV

Note that the usecols argument includes the column we want to set to the DataFrame index and nrows includes the column headers (but not the skipped rows).

The whitespace around the column names is not automatically removed. pandas provides a variety of methods for manipulating strings within the str "accessor" namespace, which can be applied to all the column names in one statement; this is faster than using rename:

df.rename(columns=lambda s: s.strip(), inplace=True)