计算布尔列的百分比

mftmpeh8  于 2021-06-24  发布在  Pig
关注(0)|答案(2)|浏览(362)

假设我的数据具有以下结构:

Year      | Location | New_client 

2018      | Paris    | true
2018      | Paris    | true
2018      | Paris    | false
2018      | London   | true
2018      | Madrid   | true
2018      | Madrid   | false
2017      | Paris    | true

我试图计算每一年和每一个地点的新客户的真实价值的百分比,因此从结构示例中获取记录的示例是

2018     | Paris    | 66
2018     | London   | 100
2018     | Madrid   | 50
2017     | Paris    | 100

适应https://stackoverflow.com/a/13484279/2802552 我目前的脚本是,但不同的是,它使用的不是1列而是2列(年份和位置)

data = load...
grp = group inpt by Year; -- creates bags for each value in col1 (Year)
result = FOREACH grp {
    total = COUNT(data);
    t = FILTER data BY New_client == 'true'; --create a bag which contains only T values
    GENERATE FLATTEN(group) AS Year, total AS TOTAL_ROWS_IN_INPUT_TABLE, 100*(double)COUNT(t)/(double)total AS PERCENTAGE_TRUE_IN_INPUT_TABLE;
};

问题是这是使用作为参考年,而我需要它是年和地区。
谢谢你的帮助。

rks48beu

rks48beu1#

你需要两人一组 Year 以及 Location ,这将需要两个修改。首先,添加 Location 按语句分组。第二,改变 FLATTEN(group) AS YearFLATTEN(group) AS (Year, Location)group 现在是一个包含两个字段的元组。

grp = group inpt by (Year, Location);
result = FOREACH grp {
    total = COUNT(inpt);
    t = FILTER inpt BY New_client == 'true';
    GENERATE 
        FLATTEN(group) AS (Year, Location), 
        total AS TOTAL_ROWS_IN_INPUT_TABLE, 
        100*(double)COUNT(t)/(double)total AS PERCENTAGE_TRUE_IN_INPUT_TABLE;
};
eanckbw9

eanckbw92#

测试了此代码,看起来很适合我:

A = LOAD ...
B = GROUP A BY (year, location);
C = FOREACH B  {
    TRUE_CNT = FILTER A BY (chararray)new_client == 'true';
    GENERATE group.year, group.location, (int)((float)COUNT(TRUE_CNT) / COUNT(A) * 100);
}

DUMP C;
(2017,Paris,100)
(2018,Paris,66)
(2018,London,100)
(2018,Madrid,50)

相关问题