如何将 pandas 的“ GROUPBY”函数的结果转换为原始数据帧 [英] How to transform the result of a Pandas `GROUPBY` function to the original dataframe

查看:145
本文介绍了如何将 pandas 的“ GROUPBY”函数的结果转换为原始数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有6列的Pandas DataFrame和一个自定义函数,该函数接受2或3列中元素的计数并产生布尔输出。从原始数据帧创建 groupby 对象并应用自定义函数 df.groupby('col1')。apply(myfunc),结果是一个序列,其长度等于 col1 的类别数。如何扩展此输出以匹配原始数据帧的长度?我尝试了 transform ,但无法将自定义函数 myfunc 与它一起使用。

Suppose I have a Pandas DataFrame with 6 columns and a custom function that takes counts of the elements in 2 or 3 columns and produces a boolean output. When a groupby object is created from the original dataframe and the custom function is applied df.groupby('col1').apply(myfunc), the result is a series whose length is equal to the number of categories of col1. How do I expand this output to match the length of the original dataframe? I tried transform, but was not able to use the custom function myfunc with it.

编辑:

下面是示例代码:

A = pd.DataFrame({'X':['a','b','c','a','c'], 'Y':['at','bt','ct','at','ct'], 'Z':['q','q','r','r','s']})
print (A)

def myfunc(df):
    return ((df['Z'].nunique()>=2) and (df['Y'].nunique()<2))

A.groupby('X').apply(myfunc)

我想将此输出扩展为新列结果,使得如果X列中有 a ,则结果将为 True

I would like to expand this output as a new column Result such that where there is a in column X, the Result will be True.

推荐答案

您可以地图 groupby返回原始数据帧

You can map the groupby back to the original dataframe

A['Result'] = A['X'].map(A.groupby('X').apply(myfunc))

结果如下:

    X   Y   Z   Result
0   a   at  q   True
1   b   bt  q   False
2   c   ct  r   True
3   a   at  r   True
4   c   ct  s   True

这篇关于如何将 pandas 的“ GROUPBY”函数的结果转换为原始数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆