-1

I want to select both the rows 489-493 and the rows 503-504 in this dataframe. I can slice them separately by df.iloc[489:493] and df.iloc[503:504], respectively, but am not sure how to combine them?

enter image description here

I have tried using df[(df.State =='Washington') & (df.State=='Wisconsin')] , however, I'm getting an empty dataframe with the column labels only. if I do only one of them, eg. df[df.State =='Washigton'] this works fine, to produce 5 rows with Washington as expected. So how can I combine them?

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • What's your expected output – Umar.H Mar 13 '20 at 22:38
  • 7
    ```df[(df.State =='Washington') |(df.State=='Wisconsin')]```? It should be ```or``` not ```and``` – Grzegorz Skibinski Mar 13 '20 at 22:38
  • 3
    Or, alternatively, `df[df.State.isin(('Washington', 'Wisconsin'))]`. – Igor Raush Mar 13 '20 at 23:11
  • Please provide a [mcve], at the very least share your data in a more convenient format. Images are not great for this kind of information, see https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Mar 14 '20 at 00:11

2 Answers2

0

use pandas.DataFrame.loc.

df = df.loc[['Washington','Wisconsin'],['Region Name']]
hula-hula
  • 119
  • 1
  • 11
0

df.iloc[np.r_[489:493, 503:504], :] worked for me!

Bluetail
  • 1,093
  • 2
  • 13
  • 27