Your first approach is solid, but here is a simple option:
df[df['Married'] & df['OwnsHouse']].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Or, just the age:
df.loc[df['Married'] & df['OwnsHouse'], 'Age'].max()
# 35
If you have multiple boolean columns, I'd suggest something a bit more scalable,
df[df[['Married', 'OwnsHouse']].all(axis=1)].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Where,
df[['Married', 'OwnsHouse']].all(axis=1)
0 False
1 True
2 False
3 True
dtype: bool
Which is the same as,
df['Married'] & df['OwnsHouse']
0 False
1 True
2 False
3 True
dtype: bool
But instead of manually finding the AND of N boolean masks, have .all
do it for you.
query
is another option:
df.query("Married and OwnsHouse")['Age'].max()
# 35
It doesn't require an intermediate step of computing a mask.
Your method is fast enough, but if you want to microoptimize, here are some more options with numpy:
# <= 0.23
df[(df['Married'].values & df['OwnsHouse'].values)].max()
df[df[['Married', 'OwnsHouse']].values.all(axis=1)].max()
# 0.24+
df[(df['Married'].to_numpy() & df['OwnsHouse'].to_numpy())].max()
df[df[['Married', 'OwnsHouse']].to_numpy().all(axis=1)].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Although you probably want just the age. Do this
df.loc[(df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()), 'Age'].max()
# 35
If you fancy some more numpy, do this:
df.loc[(
df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()), 'Age'
].to_numpy().max()
# 35
Or better still, throw away pandas,
df['Age'].to_numpy()[df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()].max()
# 35