如何在 python pandas 中使用 max 函数我得到错误的行数

问题描述 投票:0回答:1

这是我的数据集。

ID 年龄 地区 国籍 是高血压 是糖尿病2型 重量 身高
123 40 ABB 美国 1 90 170
123 40 ABB 美国 1 90 170

我需要我的数据集是这样的(唯一 ID):

ID 年龄 地区 国籍 是高血压 是糖尿病2型 重量 身高
123 40 ABB 美国 1 1 90 170

我试过这段代码

user = df.groupby(['ID', 'Age', 'Region', 'nationality','Weight','Height'], as_index=False).max()

这段代码的结果给我的行数比我运行这段代码之前的唯一 ID 数少! 行必须匹配唯一 ID 的数量不低于也不高于

python pandas
1个回答
0
投票

你可以尝试这样的事情:

cols_max = ['IsHypertension', 'IsDiabetesType2', 'Weight', 'height']
user = df.groupby(
    ['ID', 'Age', 'Region', 'nationality'], 
    as_index=False
).agg(
    {col: 'max' for col in cols_max}
)
© www.soinside.com 2019 - 2024. All rights reserved.