1

这是集合分析中的一个典型和/或问题,我被困了很长时间。

在此处输入图像描述

我想总结这些 id 的金额,其中:

  1. type_of_entry 是“收入和费用”或“收入和劳动力”

  2. 收入类型为“CAF”

预期的 ID 以粗体显示

例如...id 1 存在于收入和费用中。同样,id 2 和 3 同时存在于收入和劳动力中。

结果 -> 金额 = 55 (5+40+10)

我已经尝试了以下集合分析,但没有工作:

在此处输入图像描述

我将不胜感激这方面的任何帮助。

问候

萨格尼克

4

3 回答 3

2

脚本 -

在此处输入图像描述

p() 函数根据您的过滤器提取可能的值,在这种情况下,它是费用和人工,并且 * 运算符执行 and 操作。简而言之,您可以拥有所有所需的 ids ,然后应用收入类型过滤器。

同样,有一个 e() 函数可以提取排除的值。

这个答案不是我的,Sunny Talwar 先生帮助我找到了这个问题的解决方案。有效。

于 2020-06-09T02:23:28.713 回答
1

谢谢你的例子,它更容易理解。

我建议您首先获取串联 id 的值。例如:

Concat(<type_of_entry={'Expense','Labor'}>id, ',')

现在你可以得到这个 id 的总和,所以:

Sum(<id={"$(=Concat(<type_of_entry={'Expense','Labor'}>id, ','))"},revenue_type={'CAF'}> amount)

这应该有效,我没有验证它适用于任何数据集,但它应该!

祝你今天过得愉快!

于 2020-06-25T16:24:49.683 回答
1

您接受 Python 解决方案的答案吗?

蟒蛇解决方案

import pandas as pd
from collections import defaultdict

df = pd.DataFrame([
    ['Expense', 1, 10, '-'],
    ['Labor', 2, 20, '-'],
    ['Labor', 3, 50, '-'],
    ['Revenue', 1, 5, 'CAF'],
    ['Revenue', 2, 30, 'NORM'],
    ['Revenue', 2, 40, 'CAF'],
    ['Revenue', 3, 10, 'CAF'],
    ['Revenue', 4, 20, 'NORM'],
    ['Revenue', 5, 30, 'CAF']
], columns=['type_of_entry', 'id', 'amount', 'revenue_type'])

series_caf = df[df['revenue_type'].eq('CAF')]
filter_id_list = series_caf['id'].to_list()  # 1, 2, 3, 5
result_amount = 0
dict_ok = defaultdict(list)
for cur_id in filter_id_list:
    is_revenue = len(df[(df.id == cur_id) & (df.type_of_entry == 'Revenue')]) > 0
    is_expense = len(df[(df.id == cur_id) & (df.type_of_entry == 'Expense')]) > 0
    is_labor = len(df[(df.id == cur_id) & (df.type_of_entry == 'Labor')]) > 0
    is_ok = (is_revenue and is_expense) or (is_revenue and is_labor)
    if is_ok:
        cur_amount = series_caf[series_caf.id == cur_id].amount.values[0]
        result_amount += cur_amount
        dict_ok['id'].append(cur_id)
        dict_ok['amount'].append(cur_amount)
        dict_ok['ok_reason (REL)'].append(is_revenue*100+is_expense*10+is_labor)

df_result_info = pd.DataFrame.from_dict(dict_ok)
print(df_result_info)
print(result_amount)

输出

   id  amount  ok_reason (REL)
0   1       5              110
1   2      40              101
2   3      10              101
55
于 2020-06-09T01:57:25.697 回答