简体   繁体   English

Pandas groupby -- 根据另一列的最大值得到 output 值

[英]Pandas groupby -- get output value based on max value of another column

I have the following dataframe:我有以下 dataframe:

df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
                              'Parrot', 'Parrot'],
                   'Habitat':['Jungle', 'Jungle',
                              'Sky', 'Sky'],
                   'Tmp':['A', 'B', 'C', 'D'],
                   'Max Speed': [380., 370., 24., 26.]})

>>> df
   Animal Habitat Tmp  Max Speed
0  Falcon  Jungle   A      380.0
1  Falcon  Jungle   B      370.0
2  Parrot     Sky   C       24.0
3  Parrot     Sky   D       26.0

I am trying to add additional column "Output" which will take the value from "Tmp" based on maximum value of column "Max Speed" in a groupby done of columns "Animal" and "Habitat".我正在尝试添加额外的列“输出”,它将根据“动物”和“栖息地”列的分组中“最大速度”列的最大值从“Tmp”中获取值。

Desired output:所需的 output:

   Animal Habitat Tmp  Max Speed Output
0  Falcon  Jungle   A      380.0      A
1  Falcon  Jungle   B      370.0      A
2  Parrot     Sky   C       24.0      D
3  Parrot     Sky   D       26.0      D

It can be done using a groupby and then joining it in the original dataset.可以使用groupby ,然后将其加入原始数据集中。 But is there a more efficient way to do this?但是有没有更有效的方法来做到这一点? Maybe using transform or something else?也许使用transform或其他东西?

You can define a function taking pd.dataframe as argument:您可以定义一个 function 以pd.dataframe作为参数:

import pandas as pd
import numpy as np

def fmax(df_):
    df_['Output'] = df_.sort_values(['Max Speed']).tail(1)['Tmp'].squeeze()
    return df_

Please note use of pandas.DataFrame.squeeze function to return scalar value.请注意使用pandas.DataFrame.squeeze function 返回标量值。 Then simply apply above function using groupby :然后简单地使用groupby在 function 之上apply

df.groupby(['Animal','Habitat']).apply(fmax)

The result is:结果是:

   Animal Habitat Tmp  Max Speed Output
0  Falcon  Jungle   A      380.0      A
1  Falcon  Jungle   B      370.0      A
2  Parrot     Sky   C       24.0      D
3  Parrot     Sky   D       26.0      D

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

相关问题 如何根据 pandas.groupby().max() 中一列的最大值获取整行? - How to get the whole row based on a max value from one column in pandas.groupby().max()? python:pandas:如何基于groupby另一列在列中查找最大值 - python: pandas: how to find max value in a column based on groupby another column Pandas groupby 标识另一列中具有最大值的元素 - Pandas groupby with identification of an element with max value in another column Pandas Dataframe - GroupBy 键并将最大值保留在另一列 - Pandas Dataframe - GroupBy key and keep max value on a another column Pandas groupby 基于列值 - Pandas groupby based on column value Pandas Groupby:根据另一列的值从组的前一个元素中获取值 - Pandas Groupby: get value from previous element of a group based on value of another column 在 pandas 中 groupby 之后的列上应用条件,然后聚合以获得 2 个最大值 - Apply condition on a column after groupby in pandas and then aggregate to get 2 max value Pandas groupby 获取另一列最小的列的值 - Pandas groupby get value of a column where another column is minimum 如何在 Pandas 中的另一列分组后获取列值的总和? - How to Get Sum of column value, after groupby another column in Pandas? Pandas Dataframe:groupby id查找最大列值并返回另一列的对应值 - Pandas Dataframe: groupby id to find max column value and return corresponding value of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM