如何根据另一列的条件查找一列的最小值和最大值

时间:2020-11-01 17:22:46

标签: pandas

我正在尝试根据具有Infection_Yes == 1的值查找最小和最大“年龄”

这是我正在查看的内容:

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

我想找到最大和最小年龄(基本上只是那些具有Infection_Yes == 1的人的年龄范围)

1 个答案:

答案 0 :(得分:0)

下次,请添加样本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())

如果我错过了您的问题的重点,或者您需要我澄清任何事情,请告诉我。 祝你好运!