In [1]:
import pandas as pd
In [2]:
person = {
    "first": ["Kushagra", "Jane", "John"],
    "last": ["Gupta", "Doe", "Doe"],
    "email": ["Kushagra225@gmail.com", "JaneDoe123@gmail.com", "John@gmail.com"]
}
In [3]:
df = pd.DataFrame(person)
In [4]:
df
Out[4]:
first last email
0 Kushagra Gupta Kushagra225@gmail.com
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com
In [6]:
df['last'] == 'Doe'
Out[6]:
0    False
1     True
2     True
Name: last, dtype: bool
In [7]:
#making filter
filt = (df['last'] == 'Doe')
In [9]:
#applying filter
df[filt]
Out[9]:
first last email
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com
In [10]:
#another way for applying filter
df.loc[filt]
Out[10]:
first last email
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com
In [12]:
#applying filter and get particular column using loc
df.loc[filt, 'email']
Out[12]:
1    JaneDoe123@gmail.com
2          John@gmail.com
Name: email, dtype: object
In [15]:
# & - and , | - or
#using and
filt = (df['last'] == 'Doe') & (df['first'] == 'John')
df.loc[filt]
Out[15]:
first last email
2 John Doe John@gmail.com
In [16]:
#using or
filt = (df['last'] == 'Doe') | (df['first'] == 'John')
df.loc[filt]
Out[16]:
first last email
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com
In [18]:
#using negation (~) (opposite of normal output)
df.loc[~filt]
Out[18]:
first last email
0 Kushagra Gupta Kushagra225@gmail.com
In [20]:
#applying filter from list
first_name = ['Jane', 'John']
filt = df['first'].isin(first_name)
df.loc[filt]
Out[20]:
first last email
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com