Home > Backend Development > Python Tutorial > How to Select Specific Rows in Pandas DataFrames Based on Column Values?

How to Select Specific Rows in Pandas DataFrames Based on Column Values?

Susan Sarandon
Release: 2024-12-26 13:20:10
Original
317 people have browsed it

How to Select Specific Rows in Pandas DataFrames Based on Column Values?

Selecting Rows Based on Column Values in Pandas DataFrames

When working with Pandas DataFrames, there often arises a need to filter rows based on specific values in a particular column. This mimics SQL queries where rows are retrieved using filters like WHERE column_name = some_value.

Scalar Values

To select rows where a column value matches a scalar value, some_value, use the equality operator ==:

df.loc[df['column_name'] == some_value]
Copy after login

Iterable Values

To select rows where a column value is in an array, some_values, use the isin method:

df.loc[df['column_name'].isin(some_values)]
Copy after login

Combining Conditions

Multiple conditions can be combined using the logical & operator:

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]
Copy after login

Note: Use parentheses to ensure operator precedence is correct.

Negations

To select rows where a column value does not equal some_value, use the inequality operator !=:

df.loc[df['column_name'] != some_value]
Copy after login

For isin, negate the result using ~:

df = df.loc[~df['column_name'].isin(some_values)]
Copy after login

Examples

Consider the following DataFrame:

import pandas as pd

df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
                   'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
                   'C': np.arange(8),
                   'D': np.arange(8) * 2})
print(df)
Copy after login

Select rows where A is foo:

print(df.loc[df['A'] == 'foo'])
Copy after login

Select rows where B is one or three:

print(df.loc[df['B'].isin(['one', 'three'])])
Copy after login

Create an index and select rows using it:

df = df.set_index(['B'])
print(df.loc['one'])
Copy after login

Select rows with multiple indexed values:

print(df.loc[df.index.isin(['one', 'two'])])
Copy after login

The above is the detailed content of How to Select Specific Rows in Pandas DataFrames Based on Column Values?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template