In [1]:
import pandas as pd
import numpy as np
In [2]:
pd.__version__, np.__version__
Out[2]:
In [3]:
# needs two steps
# one to assign the dataframe to a variable
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan]
})
df
Out[3]:
In [4]:
# another one to perform the filter
df[df['country']=='USA']
Out[4]:
In [5]:
pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan]
}).query("country == 'USA'")
Out[5]:
In [ ]:
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan]
})
df.query('country.isnull()')
In [6]:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan],
'age':[23,45,45]
})
target_age = 45
df.query('age == @target_age')
Out[6]:
In [34]:
import pandas as pd
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK', 'USA'],
'age':[23,45,45]
})
df
Out[34]:
In [23]:
df.query("(name=='john') or (country=='UK')")
Out[23]:
In [35]:
import pandas as pd
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK', 'USA'],
'age':[23,45,45]
})
df
Out[35]:
In [36]:
df.query("(country=='USA') and (age==23)")
Out[36]:
In [37]:
import pandas as pd
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK', 'USA'],
'age':[23,45,45]
})
df
Out[37]:
In [38]:
names_array = ['john','anna']
df.query('name in @names_array')
Out[38]:
In [32]:
import pandas as pd
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK', 'USA'],
'age':[23,45,45]
})
df
Out[32]:
In [33]:
invalid_array = ['anna']
df.query('name not in @invalid_array')
Out[33]:
In [ ]:
import pandas as pd
df = pd.DataFrame({
'name':['john','david','anna'],
'country of birth':['USA','UK', 'USA'],
'age':[23,45,45]
})
df
In [9]:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan]
})
df
Out[9]:
In [10]:
df.query('country.isnull()')
Out[10]:
In [11]:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'name':['john','david','anna'],
'country':['USA','UK',np.nan]
})
df
Out[11]:
In [12]:
df.query('country.notnull()')
Out[12]:
In [ ]: