23

I want to select data from my CSV file.

Though I can get a data in which column

"House" == 1 (any single number) 

as following, I don't know how to get the data where

"House" in [1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156].
df = pd.read_csv('../../data/training_dataset_500.csv')
df[df['House']==1]

enter image description here

galath
  • 5,717
  • 10
  • 29
  • 41
Suzuki Soma
  • 519
  • 1
  • 8
  • 16
  • I tried df[df['House']==[1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156]] – Suzuki Soma Jul 23 '15 at 18:24
  • I also tried df[df['House']==1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156] – Suzuki Soma Jul 23 '15 at 18:24
  • Sorry i am a beginner.. – Suzuki Soma Jul 23 '15 at 18:25
  • The problem is that you ask a ton of questions since yesterday without first searching for the answer. – Julien Marrec Jul 23 '15 at 18:29
  • Duplicate of [how to filter the dataframe rows of pandas by "within"/"in"?](http://stackoverflow.com/questions/12065885/how-to-filter-the-dataframe-rows-of-pandas-by-within-in) and [this one](http://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe) – Julien Marrec Jul 23 '15 at 18:31
  • yeah.. true... I am also searching but I cannot find it..I will try to search infomation by myself. sorry.. – Suzuki Soma Jul 23 '15 at 18:31
  • Try googling "pandas filter column value list" or using the SO search box. Also you should post specific titles to the problem you're having – Julien Marrec Jul 23 '15 at 18:32
  • I'm voting to close this question, there are already two duplicates out there – Julien Marrec Jul 23 '15 at 18:33
  • Sorry I will close it. thank you Anand – Suzuki Soma Jul 23 '15 at 18:35
  • The referenced questions are not answers to this question. This question asks how to use the in operator. In pandas in and is_in are NOT the same. – user48956 Aug 01 '18 at 21:37
  • Its annoying when people vote close before understanding the question. There *is* a correct answer to this question (which is not the one marked correct). Now I can't provide it. – user48956 Aug 01 '18 at 21:38

1 Answers1

51

Use the Series.isin() method to check if a series value is in a list of values. In your case -

df[df['House'].isin([1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156])]

Example -

In [77]: df
Out[77]:
   A  B
0  1  5
1  2  6
2  3  7
3  4  8

In [78]: df[df['A'].isin([1,2])]
Out[78]:
   A  B
0  1  5
1  2  6
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176