How to filter rows based on a column value in a Pandas DataFrame?
Published on Aug. 22, 2023, 12:19 p.m.
To filter rows based on a column value in a Pandas DataFrame, you can use boolean indexing. Here is an example:
import pandas as pd
# Create a sample DataFrame with columns 'A' and 'B'
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# Filter rows where the value in column 'B' is greater than 4
filtered_df = df[df['B'] > 4]
# Print the filtered DataFrame
print(filtered_df)
This will output the following filtered DataFrame:
A B
1 2 5
2 3 6
In this example, we first created a DataFrame with columns ‘A’ and ‘B’. We then used boolean indexing to filter rows where the value in column ‘B’ is greater than 4. The resulting DataFrame contains only the rows where this condition is true.
You can also filter based on multiple conditions by combining them with the &
(and) or |
(or) operators. For example, to filter rows where the value in column ‘A’ is greater than 1 AND the value in column ‘B’ is less than 6, you can use the following code:
# Filter rows where the value in column 'A' is greater than 1 AND the value in column 'B' is less than 6
filtered_df = df[(df['A'] > 1) & (df['B'] < 6)]
# Print the filtered DataFrame
print(filtered_df)
This will output the following filtered DataFrame:
A B
1 2 5
Note that the conditions inside the parentheses are evaluated separately and then combined using the &
operator.