Filtering data

Python for MATLAB Users

Justin Kiggins

Product Manager

Comparison operators and NumPy arrays

data = np.array([0.967, 0.56, 0.171, 0.872])
threshold = 0.85

meets_criteria = data > threshold
print(meets_criteria)
[True, False, False, True]
Python for MATLAB Users

Filtering NumPy arrays

data = np.array([-1, 0.56, -1, 0.872, 1.26])

is_valid = data >= 0
valid_data = data[is_valid]
print(valid_data)
[0.56, 0.872, 1.26]
Python for MATLAB Users

Filtering DataFrames

monkeys = df['animal'] == 'monkey'
bears = df['animal'] == 'bear'

monkey_weight = df[monkeys]['weight'].mean() bear_weight = df[bears]['weight'].mean()
print(monkey_weight)
35.0
print(bear_weight)
800.0
Python for MATLAB Users

Let's practice

Python for MATLAB Users

Preparing Video For Download...