In this article, let’s see how to filter rows based on column values. Query function can be used to filter rows based on column values.
Consider below Dataframe:
Python3
import pandas as pd
data = [[ 'A' , 10 ], [ 'B' , 15 ], [ 'C' , 14 ], [ 'D' , 12 ]]
df = pd.DataFrame(data, columns = [ 'Name' , 'Age' ])
df |
Output:
Now, Suppose You want to get only persons that have Age >13. We can use Query function of Pandas.
Python3
df.query( "Age>13" )
|
Output:
Now, If you want multiple columns. For example, you want to have Age >13 and Name = C. Then,
Python3
df.query( "Age>13 and Name=='C'" )
|
Output: