将功能应用于 pandas 分组 [英] Apply function to pandas groupby

查看:75
本文介绍了将功能应用于 pandas 分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,其中的列名为my_labels,其中包含字符串:'A', 'B', 'C', 'D', 'E'.我想计算每个字符串的出现次数,然后将计数的数量除以所有计数的总和.我正在尝试像这样在Pandas中做到这一点:

I have a pandas dataframe with a column called my_labels which contains strings: 'A', 'B', 'C', 'D', 'E'. I would like to count the number of occurances of each of these strings then divide the number of counts by the sum of all the counts. I'm trying to do this in Pandas like this:

func = lambda x: x.size() / x.sum()
data = frame.groupby('my_labels').apply(func)

此代码引发错误,"DataFrame对象没有属性" size".如何在Pandas中应用函数进行计算?

This code throws an error, 'DataFrame object has no attribute 'size'. How can I apply a function to calculate this in Pandas?

推荐答案

apply采用一个函数来应用于每个值,而不是序列,并接受kwarg. 因此,这些值没有.size()方法.

apply takes a function to apply to each value, not the series, and accepts kwargs. So, the values do not have the .size() method.

也许这行得通:

from pandas import *

d = {"my_label": Series(['A','B','A','C','D','D','E'])}
df = DataFrame(d)


def as_perc(value, total):
    return value/float(total)

def get_count(values):
    return len(values)

grouped_count = df.groupby("my_label").my_label.agg(get_count)
data = grouped_count.apply(as_perc, total=df.my_label.count())

此处的.agg()方法采用了一个函数,该函数应用于所有值. html#groupby-object-attributes>分组对象.

The .agg() method here takes a function that is applied to all values of the groupby object.

这篇关于将功能应用于 pandas 分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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