简体   繁体   English

Pandas dataframe 按列分组并在不同列上应用最小值、最大值、平均值

[英]Pandas dataframe group by column and apply min, max, average on different columns

I am looking a way to transform this dataframe below我正在寻找一种方法来转换下面的这个 dataframe

itemid  clock   value_min   value_avg   value_max   item_type
A1  27/05/2021    4             7           38          cpu
A2  27/05/2021    4             5           15          mem
B1  27/05/2021    1             2           5           cpu
B2  27/05/2021    3             20          86          mem
A1  28/05/2021    8             8           9           cpu
A2  28/05/2021    1             2           5           mem
B1  28/05/2021    0             1           2           cpu
B2  28/05/2021    4             7           8           mem

to

itemid  value_min   value_avg   value_max   item_type
A1       4           7,5          38           cpu
A2       1           3,5          15           mem
B1       0           0,5           5           cpu
B2       3           3,5          86           mem

I want to group by itemid and then apply the rules我想按 itemid 分组,然后应用规则

  • where column value_min is the min,其中 value_min 列是最小值,
  • where column value_avg is the mean其中列 value_avg 是平均值
  • where column value_max is the max其中 value_max 列是最大值

this should answer your question:这应该回答你的问题:

df = df.groupby('itemid').agg({'value_min': 'min', 'value_avg': 'mean', 'value_max': 'max', 'item_type': 'first'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM