V1

2022/01/06阅读：32主题：红绯

# Understand Pandas Indexes

## To efficiently use Pandas, ignore its documentation and learn the truth about indexes

[

](/understand-pandas-indexes-1b94f5c078c6?source=post_page-----1b94f5c078c6-----------------------------------)

The Python Pandas library is a great tool for data manipulation. However, it is only efficient if you understand Pandas indexing. Pandas indexing is the key to accessing and joining rows in seconds instead of minute or hours.

# Indexes

Like a Python dictionary (or a relational database’s index), Pandas indexing provides a fast way to turn a key into a value. For example, we can create a dataframe with index `alpha`:

and then turn the key `b` into the row of interest.

But what kind of thing is a Panda index? The documentation says an index is an

Immutable ndarray implementing an ordered, sliceable set (emphasis added)

In other words, a kind of mathematical set. Recall that mathematical set has these two important properties:

• No repeated elements
• Elements are unordered

But now, look at a second example:

We again turn the `alpha` column turned into an index. The element `x`, however, appears twice and the retrieved the rows respect the order of the two `x`’s. This illustrates that with a Pandas index:

• Elements may be repeated
• Elements are ordered

So, contrary to the Pandas documentation, a Pandas index is not a mathematical set. Instead, it is a kind of list. Specifically, a Pandas index is

• A (kind of) list of hashable elements, where
• the position(s) of elements can be found quickly.

With this knowledge, we can easily understand the basics of indexes, starting with their creation, deletion, and manipulation.

# Manipulating Indexes

The examples above showed how to turn a column into an index with `.set_index()`. We can turn the index back into a column with `.reset_index()`:

Let us put the index back and then look at all the elements inside the index. The property is `.index.values`. As expected, elements are a kind of list, specifically, a NumPy array.

We also expect to be able to quickly find the row number(s) corresponding to any index element. The method is `.index.get_loc()`. The result will be an integer or bool array, depending on the number of rows.

# Row Access

The main way to access rows with index elements is `.loc[…]` (note the square brackets), where the input can be a:

• single element
• list of elements
• slice of elements

The rows will be output in the order they appear in the input. This example shows each kind of input.

Note that unlike the rest of Python, the start:stop slice is inclusive of the stop value.

# Joining Rows

Finally, let us look at joining two datafames. The rules are:

• The left dataframe need not be indexed, but the right one does.
• Give the left column(s) of interest in the join’s `on` input.

In this example, we will use `join` to add a “score” column to a dataframe. Here is the left dataframe. It isn't indexed.

The right dataframe needs an index, but it can be named anything. Here we call it `alpha2`.

We combine the two dataframes with a left join. We use column `alpha` from the first dataframe and whatever is indexed in the second dataframe. The result is a new dataframe with a score column.

# Conclusion

We have seen that contrary to the documentation, a Pandas index is not a mathematical set. Instead, it is a kind of list with a fast way to find the position(s) of any element.

Understanding this makes it easy to understand how to create, remove and manipulate indexes. We can then use indexes to quickly access and join rows.

What’s next? With this foundation, you should next learn to create indexes from multiple columns, to apply set-like operators to indexes, and to efficiently delete rows. (Surprisingly, Pandas grouping and sorting does not need or use indexes.)

[

Ph.D. in CS and Machine Learning. Retired Microsoft & Microsoft Research. Volunteer, open-source projects related to ML and to Genomics.

• [

#### Cory Doctorow

](https://doctorow.medium.com/?source=blogrolls_sidebar-----1b94f5c078c6-----------------------------------)

• [

#### TDS Editors

](https://towardsdatascience.medium.com/?source=blogrolls_sidebar-----1b94f5c078c6-----------------------------------)

• [

#### Lessig

](https://medium.lessig.org/?source=blogrolls_sidebar-----1b94f5c078c6-----------------------------------)

• [

#### Anne Vaeth

](https://annemarievaeth.medium.com/?source=blogrolls_sidebar-----1b94f5c078c6-----------------------------------)

• [

#### Guido van Rossum

](https://medium.com/@gvanrossum_83706?source=blogrolls_sidebar-----1b94f5c078c6-----------------------------------)

See all (9)

60

Related

Thanks to Linda Chen.

### By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge research to original features you don't want to miss. Take a look.

60 claps

60

## More from Towards Data Science

Follow

Your home for data science. A Medium publication sharing concepts, ideas and codes.

[

David Yaffe

](https://dyaffe1.medium.com/?source=follow_footer-----1b94f5c078c6----0-------------------------------)[

·Sep 30, 2020

](/putting-an-end-to-unreliable-analytics-386431bb4e56?source=follow_footer-----1b94f5c078c6----0-------------------------------)

# Putting an end to Unreliable Analytics

When building a product or service, it’s imperative to know that input data will be as expected. …

[

3

Write on Medium

[

Yong Cui

](https://yongcui01.medium.com/?source=follow_footer-----1b94f5c078c6----1-------------------------------)[

·Sep 30, 2020

](/how-to-calculate-the-number-of-parameters-in-keras-models-710683dae0ca?source=follow_footer-----1b94f5c078c6----1-------------------------------)

# Introduction

Although new ML frameworks are emerging, keras models are still favorites for many data scientists. For new learners, one major question that they may have is to make sense of the model that they’re building by following particular tutorials. To put our discussion in a context, let’s suppose that we’re…

[

58

[

Guillermo Barreiro

](https://medium.com/@guille_barreiro?source=follow_footer-----1b94f5c078c6----2-------------------------------)[

·Sep 30, 2020

](/analyzing-worldwide-cuisines-with-python-and-foursquare-api-e63455c14246?source=follow_footer-----1b94f5c078c6----2-------------------------------)

# Analyzing Worldwide Cuisines With Python And Foursquare API

## Analyzing which cuisines are most popular in the biggest cities in the world

There are plenty of well-known cuisines around the world, like Chinese, Japanese, Mexican, Spanish… It might be obvious that Mexico is the best place to taste Mexican food, Thailand for Thai food, and so on, but it would be very tedious to visit so many countries to taste each different…

[

14

[

1

](/analyzing-worldwide-cuisines-with-python-and-foursquare-api-e63455c14246?responsesOpen=true&source=follow_footer-----1b94f5c078c6----2-------------------------------)

[

Bharath K

](https://bharath-k1297.medium.com/?source=follow_footer-----1b94f5c078c6----3-------------------------------)[

·Sep 30, 2020

# Neural Networks Made Fun With TensorFlow Playground!

## Using TensorFlow Playground to explore and have fun with Neural Networks

TensorFlow Playground is an extremely awesome website where you can visualize and intuitively understand how neural networks work. This website, developed by the TensorFlow team at Google, is one of the best platforms that will allow you to explore the powerful deep neural networks.

This short article will guide you…

[

211

[

Taylor Stanley

](https://tcastanley.medium.com/?source=follow_footer-----1b94f5c078c6----4-------------------------------)[

·Sep 30, 2020

](/using-neural-networks-to-improve-waste-management-one-propagation-at-a-time-b2b7414d329d?source=follow_footer-----1b94f5c078c6----4-------------------------------)

# Scope of the Problem

It’s definitely not pretty, and it’s not something brought up at the dinner table, though after each meal is over the process starts again, I’m talking about waste!

Waste management is the process of collecting and disposing of waste, and is an integral part of every society and one that…

V1