Find if something is also somewhere else (contd) | 找一找那里是不是也有

Today’s class we continued the game of finding matches.  We expanded from numbers to names.

Let’s pretend that there is a room out there that has the following famous people:

room1 = pd.Series([‘Grace Hopper’, ‘Albert Einstein’,’Michael Faraday’,’Emmy Noether’,’Ada Lovelace’])

And another room with these famous people:

room2 = pd.Series([‘Isaac Newton’, ‘Thomas Edison’,’Mary Somerville’,’Matilda’,’Ada Lovelace’])

These two rooms are in building:

building = pd.concat([room1, room2], axis=1)

building.columns= [‘room1′,’room2’]

             room1            room2
0     Grace Hopper     Isaac Newton
1  Albert Einstein    Thomas Edison
2  Michael Faraday  Mary Somerville
3     Emmy Noether          Matilda
4     Ada Lovelace     Ada Lovelace

Now we want to find a list of people “who” in the building and the rooms.

who = np.array([‘Albert Einstein’,’Michael Faraday’,’Ada Lovelace’])

Are they in room 1?

np.isin(who, room1)
Out: array([ True,  True,  True])

Are they in room2?

np.isin(who, room2)

Out: array([False, False,  True])

Are they in the building?

np.isin(who, building)
Out: array([ True,  True,  True])

We are constantly comparing things.  How to compare is a very tricky and interesting subject.  You should look up the source code of the function in1d and see how it does it.

Leave a Reply

Your email address will not be published.