簡體   English   中英

在 dataframe 中查找一組值的百分比

[英]Find percentage of set of values in a dataframe

我有一個 dataframe 之類的

business_id  FP   count 
business_1    1    20
business_1    0    100
business_2    0    50

我想計算 FP=1 的每個 business_id 的百分比。 基本上我需要一個等於 count(where business_id=some id and FP=1)/count(where business_id=same_id and FP=0)

如果該值始終為 <1>,我的快速 2 美分將是:

給定以下 dataframe:

df = pd.DataFrame([
    ['business_1', 1, 20],
    ['business_1', 0, 100],
    ['business_2', 0, 50],
], columns=['business_id', 'FP', 'count'])

計算百分比並將它們作為字典返回:

percentages = df.groupby('business_id').agg({
    'FP': ['count', 'sum']
})
(percentages[('FP', 'sum')] / percentages[('FP', 'count')]).to_dict()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM