dwool
dwool

Reputation: 29

How to find the min and max value of one column based on condition in another column

I am trying to find the min and max 'Age' based on values that had Infection_Yes==1

Here is what I am looking at:

new_df.loc[new_df['Infection_Yes'] == 1]

![Text](https://stackoverflow.com/image.jpg)

Age SEX DIABETES    bmi SMOKE   DPRALBUM    Readmission Infection   bmi_cat albumin_cat ... age_cat_lt65    SEX_female  SEX_male    Readmission_No  Readmission_Yes Infection_Yes   Infection_no    Optimized_No    Optimized_Unknown   Optimized_Yes
410 68.0    male    NON-INSULIN 25.01   No  NaN Yes Yes lt40    0   ... 1   0   1   0   1   1   0   0   1   0
724 54.0    male    NO  34.97   No  13.0    Yes Yes lt40    gt3.5   ... 1   0   1   0   1   1   0   0   0   1
736 69.0    female  NO  25.98   No  16.0    No  Yes lt40    gt3.5   ... 1   1   0   1   0   1   0   0   0   1
1228    68.0    male    NO  22.86   Yes 12.0    No  Yes lt40    gt3.5   ... 1   0   1   1   0   1   0   1   0   0
1297    75.0    female  NO  NaN No  NaN Yes Yes 0   0   ... 0   1   0   0   1   1   0   0   1   0
5 rows × 37 columns

I am wanting to find the max and min age (basically just the range of ages of those who have Infection_Yes==1)

Upvotes: 0

Views: 2571

Answers (1)

iMS44
iMS44

Reputation: 113

next time please add sample df.

# import pandas
import pandas as pd

# sample df
d = {'Infection': ['Y', 'N', 'Y', 'Y', 'N', 'Y','N'],'Age': [11, 22, 33, 44, 55, 66, 77]}
df = pd.DataFrame(data=d)

# min max age if Infection == Y
print(df[df['Infection']=='Y']['Age'].min())
print(df[df['Infection']=='Y']['Age'].max())

If I missed the point of your question or you need me to clarify anything let me know. Good luck!

Upvotes: 3

Related Questions