pandas
  1. pandas-boolean-indexing

Boolean indexing - ( Pandas and NumPy )

Heading h2

Syntax

arr[boolean_array]
df[boolean_series]

Example

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

# creating a boolean array
mask = arr > 3

# filtering array with boolean indexing
new_arr = arr[mask]

print(new_arr)
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})

# creating a boolean series based on column values
mask = df['A'] > 2

# filtering dataframe with boolean indexing
new_df = df[mask]

print(new_df)

Output

[4 5 6]
   A  B
2  3  7
3  4  8

Explanation

Boolean indexing is a technique in NumPy and Pandas for filtering arrays and dataframes using a boolean array or series. A boolean array or series is an array or series of True and False values that have the same shape as the array or series to be filtered.

In the above example, we create a NumPy array and a Pandas dataframe and filter them using boolean indexing. We create a boolean array or series by applying a condition to the original array or series. We then use this boolean array or series to filter the array or dataframe using boolean indexing.

Use

Boolean indexing is a useful technique for filtering data that meets a certain condition. It can be used in various data analysis and machine learning tasks.

Important Points

  • Boolean indexing is a technique for filtering arrays and dataframes using a boolean array or series
  • A boolean array or series consists of True and False values and has the same shape as the array or series being filtered
  • Boolean indexing can be used to filter data that meets a certain condition

Summary

In conclusion, boolean indexing is a useful technique for filtering arrays and dataframes based on a given condition. It is widely used in data analysis and machine learning tasks. Boolean indexing is a simple and powerful tool that enhances the ease of working with NumPy and Pandas.

Published on: