Calmcode - lambda: pandas


1 2 3 4 5

Note that for this code to run you need to install both numpy and pandas beforehand.

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.normal(0, 1, (10, 2)))
df.columns = ['column_a', 'column_b']
df.loc[lambda d: d['column_b'] > 0]

Note that again here the loc tells us what we're going to be taking a subset of the original dataframe while the lambda function tell us how we're going to select which rows can stay. It can be somewhat complicated to fully appreciate how this works under the hood though. We'll add a chapter on method chains in the future to dive more in depth into this phenomenon.