Monday 8 July 2019

python - How do I use within / in operator in a Pandas DataFrame?




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



Answer



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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...