Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Multilevel slicing

The good part is that given a dictionary of dataframes, pd.concat will create a multilevel column index, which will come in handy in a bit. This means, however, that it's not enough now to pass the column name as a string; we need to use multilevel slicing. Let's use an alias:

idx = pd.IndexSlice

Now, if we want to get a specific column in this dataframe, we have to use .loc with this indexing object for columns. The IndexSlice interface is very similar to loc. For one column, we'll use it like this:

df.loc[:, idx['old_metrics', 'url']]

Note that because we defined a specific value on all the levels, the result will be pandas Series. We can, however, relax our query, by using colons: for example, df.loc[:, idx[:, 'killed']] ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content