How to Select Columns Based on a Logical Condition in Pandas (Python)

preview_player
Показать описание
↓ Code Available Below! ↓

This video shows how to select columns of a data frame based on a logical condition. Filtering or subsetting the columns of a data frame based on a logical check is not quite as common as filtering rows, but it can still be useful operation if you are only interested in variables that conform to certain conditions.

If you find this video useful, like, share and subscribe to support the channel!

Code used in this Python Code Clip:

import pandas as pd

# Create a logical index with one value for each column

logical_index

# Get the corresponding columns

# Use the column list to index the data frame
mtcars_sub = mtcars[cols]

# Do logical indexing on columns in one line:

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .

Рекомендации по теме