将功能应用于 pandas 分组 [英] Apply function to pandas groupby
问题描述
我有一个熊猫数据框,其中的列名为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()
方法采用了一个函数,该函数应用于
The .agg()
method here takes a function that is applied to all values of the groupby object.
这篇关于将功能应用于 pandas 分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!