[英]Unable to use .size() .div() methods inside pandas groupby with lambda function
我正在使用以下代碼行來計算條件概率
variable = 'variable_name'
probs = df.groupby(variable).size().div(len(df))
cond_probs = df.groupby([variable, 'has_income']).size().div(len(df)).div(probs, axis=0, level=variable)
這些將導致以下輸出:
varibale_name has_income
(0.999, 2.0] False 0.756323
True 0.243677
(2.0, 3.0] False 0.798372
True 0.201628
(3.0, 16.0] False 0.809635
True 0.190365
我想在輸出中添加額外的列作為每個組的樣本大小,但是我無法在lambda函數中重寫公式,因為組對象與返回的對象沒有相同的方法通過df.groupby() 。 例:
cond_probs =df.groupby([variable, 'has_income']).apply(lambda x:
pd.Series({
'probs': x.size().div(len(df)).div(probs, axis=0, level=variable),
'size': x.size()
}))
錯誤:TypeError:“ numpy.int32”對象不可調用
是否有其他選擇可以以理想的方式獲得這些結果,而無需計算兩個groupby並在最后加入數據幀?
當將apply
與groupby
一起使用時,您不會獲得組對象,但是會得到與相關組相對應的數據框的一部分。 所以x
在你的情況下是一個DataFrame,而不是一個GroupBy
對象-對待它的方式與對待df
相同。
cond_probs = df.groupby([variable, 'has_income']).apply(lambda x:
pd.Series({
'probs': (len(x) / len(df)) / probs[x.iloc[0][variable]],
'size': len(x)
})
)
NB如果使用.size
上一個數據幀,它將返回細胞的總數-所以它不是一樣GroupBy.size
( 文檔 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.