How do I select rows from a DataFrame based on column values?
You can use the DataFrame.loc
method to select rows from a DataFrame based on column values.
Here is an example:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Select rows where column 'A' has a value greater than 1
df_subset = df.loc[df['A'] > 1]
print(df_subset)
The output of this code will be:
A B C 1 2 5 8 2 3 6 9
Watch a video course
Python - The Practical Guide
You can also use multiple conditions to select rows using the &
and |
operators to specify logical AND and OR, respectively. For example:
# Select rows where column 'A' has a value greater than 1 and column 'B' has a value less than 6
df_subset = df.loc[(df['A'] > 1) & (df['B'] < 6)]
print(df_subset)
The output of this code will be:
A B C 1 2 5 8
You can also use the .isin()
method to select rows based on whether the value in a certain column is in a list of values. For example:
# Select rows where column 'A' has a value in the list [1, 3]
df_subset = df.loc[df['A'].isin([1, 3])]
print(df_subset)
The output of this code will be:
A B C 0 1 4 7 2 3 6 9