Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use within / in operator in a Pandas DataFrame? [duplicate]

Tags:

python

pandas

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

like image 491
Suzuki Soma Avatar asked Jul 23 '15 18:07

Suzuki Soma


People also ask

What does the IN operator do in pandas?

Pandas isin() method is used to filter data frames. isin() method helps in selecting rows with having a particular(or Multiple) value in a particular column. Parameters: values: iterable, Series, List, Tuple, DataFrame or dictionary to check in the caller Series/Data Frame.

How do you duplicate in pandas?

Pandas DataFrame duplicated() Method The duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not. Use the subset parameter to specify if any columns should not be considered when looking for duplicates.

What is a correct method to discover if a row is a duplicate?

Finding duplicate rows To find duplicates on a specific column, we can simply call duplicated() method on the column. The result is a boolean Series with the value True denoting duplicate. In other words, the value True means the entry is identical to a previous one.


1 Answers

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
like image 154
Anand S Kumar Avatar answered Sep 28 '22 03:09

Anand S Kumar