To filter a Pandas dataframe, you can use the query()
function or the bracket notation to select rows that meet a certain condition.
Here's an example of using the query()
function to filter a dataframe in Pandas:
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({"A": [1, 2, 3, 4, 5],
"B": [6, 7, 8, 9, 10]})
# filter the dataframe to only include rows where column A is greater than 2
df_filtered = df.query("A > 2")
# display the result
print(df_filtered)
This will filter the dataframe to only include rows where the value in the A
column is greater than 2, and return a new dataframe with the matching rows.
The output will be:
A B
2 3 8
3 4 9
4 5 10
You can also use the bracket notation to filter a dataframe in Pandas. For example:
# filter the dataframe to only include rows where column A is greater than 2
df_filtered = df[df["A"] > 2]
# display the result
print(df_filtered)
This will filter the dataframe in the same way as before, but using the bracket notation instead of the query()
function. The output will be the same as before.
The query()
function and the bracket notation are both useful for filtering a dataframe in Pandas based on a certain condition.
Related tutorials curated for you
How to convert a series to a list in Pandas
What is .notnull in Pandas?
How to drop duplicate columns in Pandas
How to sort a series in Pandas
What is idxmax() in Pandas?
How to use pandas map() function
What does Head() do in Pandas?
How to get the absolute value for a column in Pandas
How to use str.contains() in Pandas
How to find the minimum in Pandas
How to filter a Pandas DataFrame
How to normalize a column in Pandas