8

Sorry, I just asked this question: Pythonic Way to have multiple Or's when conditioning in a dataframe but marked it as answered prematurely because it passed my overly simplistic test case, but isn't working more generally. (If it is possible to merge and reopen the question that would be great...)

Here is the full issue:

sum(data['Name'].isin(eligible_players))
> 0

sum(data['Name'] == "Antonio Brown")
> 68

"Antonio Brown" in eligible_players
> True

Basically if I understand correctly, I am showing that Antonio Brown is in eligible players and he is in the dataframe. However, for some reason the .isin() isn't working properly.

As I said in my prior question, I am looking for a way to check many ors to select the proper rows

____ EDIT ____

In[14]:
eligible_players
Out[14]:
Name
Antonio Brown       378
Demaryius Thomas    334
Jordy Nelson        319
Dez Bryant          309
Emmanuel Sanders    293
Odell Beckham       289
Julio Jones         288
Randall Cobb        284
Jeremy Maclin       267
T.Y. Hilton         255
Alshon Jeffery      252
Golden Tate         250
Mike Evans          236
DeAndre Hopkins     223
Calvin Johnson      220
Kelvin Benjamin     218
Julian Edelman      213
Anquan Boldin       213
Steve Smith         213
Roddy White         208
Brandon LaFell      205
Mike Wallace        205
A.J. Green          203
DeSean Jackson      200
Jordan Matthews     194
Eric Decker         194
Sammy Watkins       190
Torrey Smith        186
Andre Johnson       186
Jarvis Landry       178
Eddie Royal         176
Brandon Marshall    175
Vincent Jackson     175
Rueben Randle       174
Marques Colston     173
Mohamed Sanu        171
Keenan Allen        170
James Jones         168
Malcom Floyd        168
Kenny Stills        167
Greg Jennings       162
Kendall Wright      162
Doug Baldwin        160
Michael Floyd       159
Robert Woods        158
Name: Pts, dtype: int64

and

In [31]:
data.tail(110)
Out[31]:
Name    Pts year    week    pos Team
28029   Dez Bryant  25  2014    17  WR  DAL
28030   Antonio Brown   25  2014    17  WR  PIT
28031   Jordan Matthews 24  2014    17  WR  PHI
28032   Randall Cobb    23  2014    17  WR  GB
28033   Rueben Randle   21  2014    17  WR  NYG
28034   Demaryius Thomas    19  2014    17  WR  DEN
28035   Calvin Johnson  19  2014    17  WR  DET
28036   Torrey Smith    18  2014    17  WR  BAL
28037   Roddy White 17  2014    17  WR  ATL
28038   Steve Smith 17  2014    17  WR  BAL
28039   DeSean Jackson  16  2014    17  WR  WAS
28040   Mike Evans  16  2014    17  WR  TB
28041   Anquan Boldin   16  2014    17  WR  SF
28042   Adam Thielen    15  2014    17  WR  MIN
28043   Cecil Shorts    15  2014    17  WR  JAC
28044   A.J. Green  15  2014    17  WR  CIN
28045   Jordy Nelson    14  2014    17  WR  GB
28046   Brian Hartline  14  2014    17  WR  MIA
28047   Robert Woods    13  2014    17  WR  BUF
28048   Kenny Stills    13  2014    17  WR  NO
28049   Emmanuel Sanders    13  2014    17  WR  DEN
28050   Eddie Royal 13  2014    17  WR  SD
28051   Marques Colston 13  2014    17  WR  NO
28052   Chris Owusu 12  2014    17  WR  NYJ
28053   Brandon LaFell  12  2014    17  WR  NE
28054   Dontrelle Inman 12  2014    17  WR  SD
28055   Reggie Wayne    11  2014    17  WR  IND
28056   Paul Richardson 11  2014    17  WR  SEA
28057   Cole Beasley    11  2014    17  WR  DAL
28058   Jarvis Landry   10  2014    17  WR  MIA
Engineero
  • 12,340
  • 5
  • 53
  • 75
qwertylpc
  • 2,016
  • 7
  • 24
  • 34
  • 1
    Please make a [mcve], so other people can confirm the issue (and we can verify it's not just that `eligible_players` has changed somehow or something else trivial.) – DSM Oct 06 '15 at 19:55
  • yes, re the above, in my toy this works perfectly fine – Woody Pride Oct 06 '15 at 19:59
  • @DSM I tried to create a Minimal, Complete, and Verifiable Example in my previous question which I linked, and it worked so I accepted the answer. When running it on my actual data it didn't work, so I am looking for where the problem stems from... – qwertylpc Oct 06 '15 at 20:19
  • @qwertylpc: your previous question doesn't contain an MCVE. There's nothing anyone can copy and paste to see the problem you're facing. – DSM Oct 06 '15 at 20:23
  • @DSM I added parts of the dataframe and eligible_players – qwertylpc Oct 06 '15 at 20:31

1 Answers1

11

(Aside: once you posted what you were actually using, it only took seconds to see the problem.)

Series.isin(something) iterates over something to determine the set of things you want to test membership in. But your eligible_players isn't a list, it's a Series. And iteration over a Series is iteration over the values, even though membership (in) is with respect to the index:

In [72]: eligible_players = pd.Series([10,20,30], index=["A","B","C"])

In [73]: list(eligible_players)
Out[73]: [10, 20, 30]

In [74]: "A" in eligible_players
Out[74]: True

So in your case, you could use eligible_players.index instead to pass the right names:

In [75]: df = pd.DataFrame({"Name": ["A","B","C","D"]})

In [76]: df
Out[76]: 
  Name
0    A
1    B
2    C
3    D

In [77]: df["Name"].isin(eligible_players) # remember, this will be [10, 20, 30]
Out[77]: 
0    False
1    False
2    False
3    False
Name: Name, dtype: bool

In [78]: df["Name"].isin(eligible_players.index)
Out[78]: 
0     True
1     True
2     True
3    False
Name: Name, dtype: bool

In [79]: df["Name"].isin(eligible_players.index).sum()
Out[79]: 3
DSM
  • 342,061
  • 65
  • 592
  • 494