1

I have a DataFrame df which contain three columns: ['mid','2014_amt','2015_amt']

I want to extract rows of a particular merchant. For example, consider my data is:

df['mid'] = ['as','fsd','qww','fd']
df['2014_amt] = [144,232,45,121]
df['2015_amt] = [676,455,455,335]

I want to extract the whole rows corresponding to mid = ['fsd','qww'] How is this best done? I tried with the below code:

df.query('mid== "fsd"')

If I want to run a loop, how can I use the above code to extract rows for specified values of mid?

for val in mid:
           print df.query('mid' == "val"')

This is giving an error, as val is not specified.

Ezra Citron
  • 101
  • 2
  • 6
neha
  • 1,858
  • 5
  • 21
  • 35

1 Answers1

2

Option 1

df.query('mid in ["fsd", "qww"]')

   mid  2014_amt  2015_amt
1  fsd       232       455
2  qww        45       455

Option 2

df[df['mid'].isin(['fsd', 'qww'])]

   mid  2014_amt  2015_amt
1  fsd       232       455
2  qww        45       455
piRSquared
  • 285,575
  • 57
  • 475
  • 624