0

I'm learning dataframe now. I've been stuck in how to get a subset of a dataframe or table with its label index. I know it's a very simple question but I couldn't find the solution in pandas documentation. Hope someone could help me. Appreciate your help.

So, I have a dataframe named df_teams like below: enter image description here

If I want to get a subtable of a specific team 'Warriors', I can use df_teams[df_teams['nickname']=='Warriors'], resulting a row in the form of dataframe. My question is, what if I want to get a subtable of more teams, say I want information of both 'Warriors' and 'Hawks' to form a new table? Can I do something similar by using logical index and finishing in one line of code?

joeW
  • 29
  • 5
  • Can you provide a reproducible example instead of posting a screenshot? https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – william3031 Nov 25 '20 at 04:21

1 Answers1

0

You could do a bitwise or on the two conditions using the '|' character.

df_teams[(df_teams['nickname']=='Warriors')|(df_teams['nickname']=='Hawks')]

Alternatively if you have a list of values you want to check against you could instead use the isin method to return rows that have one of the values present in the list.

E.g

df_teams[df_teams['nickname'].isin(['Warriors','Hawks'])]
Sector97
  • 116
  • 9