**Pandas**

655 guides

*chevron_left*

**Selecting Data Cookbook**

Accessing a single value of a DataFrameAccessing columns of a DataFrame using column labelsAccessing columns of a DataFrame using integer indicesAccessing rows of a DataFrame using integer indicesAccessing rows of a DataFrame using row labelsAccessing the first n rowsAccessing the last n rowsAccessing values of a multi-index DataFrameAdding prefix to column labelsAdding suffix to column labelsConverting two columns into a dictionaryExcluding columns based on typeGetting earliest or latest date from DataFrameGetting every nth rowGetting indexes of rows matching conditionsGetting shortest and longest stringsSelecting a single column as a DataFrameSelecting columns of a DataFrame using regexSelecting last column of DataFrameSelecting rows based on a conditionExtracting values of a DataFrame as a Numpy arrayGetting a list of all the column labelsGetting all columns except oneGetting all duplicate rowsGetting all numeric columns of a DataFrameGetting all unique values of columnsGetting column label of max value in each rowGetting column label of minimum value in each rowGetting columns by data typeGetting columns using integer indexGetting first row value of a columnGetting index of Series where value is TrueGetting integer index of a column using its column labelGetting integer index of rows based on column valuesGetting multiple columnsGetting row with largest index valueGetting row with smallest index valueGetting rows based on multiple column valuesGetting rows except someGetting rows from a DataFrame based on column valuesGetting rows that are not in other DataFrameGetting rows using OR statementGetting rows where column values are of specific lengthGetting rows where value is between two valuesGetting rows where values do not contain substringGetting the column labels of a DataFrameGetting the first columnGetting the index of a DataFrameGetting the length of the longest string in a columnGetting the longest string in a columnGetting the row with the maximum column valueGetting the row with the minimum column valueGetting the shape of a DataFrameGetting number of columns of a DataFrameGetting the total number of rows of a DataFrameGetting the total number of values in a DataFrameMaking column labels all lowercaseMaking column labels all uppercaseRandomly select rows based on a conditionRandomly selecting n columns from a DataFrameRandomly selecting n rows from a DataFrameReassigning column valuesRetrieving DataFrame column values as a NumPy arraySelecting columns that do not begin with certain prefixSelecting columns with certain prefixSelecting n rows with the smallest values for a columnSelecting rows based on datesSelecting rows from a DataFrame whose column values are contained in a listSelecting rows from a DataFrame whose column values are NOT contained in a listSelecting rows from a DataFrame whose column values contain a substringSelecting rows starting with substringSelecting top n rows with the largest values for a columnSplitting DataFrame based on column values

check_circle

Mark as learned thumb_up

0

thumb_down

0

chat_bubble_outline

0

Comment auto_stories Bi-column layout

settings

# Selecting rows based on a condition in Pandas

*schedule*Aug 10, 2023

local_offer

Tags Python●Pandas

*toc*Table of Contents

*expand_more*

Master the

Start your free 7-days trial now!

**mathematics behind data science**with 100+ top-tier guidesStart your free 7-days trial now!

Consider the following DataFrame:

```
df
A Ba 3 6b 4 7c 5 8
```

Here's our boolean mask:

```
mask = [False, True, True]
```

To extract rows from `df`

that correspond to `True`

in the mask, simply use the `[~]`

notation:

```
df[mask]
A Bb 4 7c 5 8
```

You could also use the `loc`

property like so:

```
mask = [False, True, True]
A Bb 4 7c 5 8
```

# Creating a boolean mask based on conditions

Consider the same DataFrame as above:

```
df
A Ba 3 6b 4 7c 5 8
```

Create a mask based on condition column `A == 4`

:

```
mask = df["A"] == 4print(mask)
a Falseb Truec FalseName: A, dtype: bool
```

Apply the mask:

```
df[mask] #same as df[df["A"] == 4]
A Bb 4 7
```

Notice the resulting DataFrame only returns the row where the value in column A is 4 (where the condition is met).

## isin method

Consider the following DataFrame:

```
```

To return rows where the column value of A is either 1, 2 or 3:

```
```

## Multiple conditions

Consider the following DataFrame:

```
df
A Ba 3 6b 4 7c 5 8
```

To retrieve rows where `"A" >= 4`

and `"B" > 7`

:

```
mask = (df["A"] >= 4) & (df["B"] > 7)
A Bc 5 8
```

Published by Isshin Inada

Edited by 0 others

Did you find this page useful?

thumb_up

thumb_down

Comment

Citation

Ask a question or leave a feedback...

thumb_up

0

thumb_down

0

chat_bubble_outline

0

settings

Enjoy our search

Hit / to insta-search docs and recipes!