遍历所有可能的组合,对另一列求和。

huangapple go评论66阅读模式
英文:

Iterate Thru All Possible Combinations, Summing Another Column

问题

我有一个如下的数据框:

用户ID 销售额 样本标志_1 样本标志_2 四分位数_1 四分位数_2
1 10 0 1 1 1
2 21 1 1 2 2
3 300 0 1 3 3
4 41 0 1 4 4
5 55 0 1 1 1

...

我试图迭代所有可能的组合(在我的示例中是example_flag_1,example_flag_2,quartile_1和quartile_2)。然后,对于每个组合,符合该组合配置的用户的销售总额是多少?

例如,对于所有样本标志和四分位数均为1的用户,他们的销售总额是多少?
对于0,1,1,1呢?
我希望计算机遍历所有可能的组合并告诉我。

我希望这很清楚,但如果你有任何问题,请告诉我。

英文:

I have a dataframe such as below:

user_id sales example_flag_1 example_flag_2 quartile_1 quartile_2
1 10 0 1 1 1
2 21 1 1 2 2
3 300 0 1 3 3
4 41 0 1 4 4
5 55 0 1 1 1

...

I'm attempting to iterate through all possible combinations of (in my example) example_flag_1, example_flag_2, quartile_1, and quartile_2. Then, for each combination, what is the sum of sales for users who fit that combination profile?

For example, for all users with 1, 1, 1, 1, what is the sum of their sales?
What about 0, 1, 1, 1?
I want the computer to go through all possible combinations and tell me.

I hope that's clear, but let me know if you have any questions.

答案1

得分: 1

使用 itertools.product() 生成组合,使用 functools.reduce() 生成掩码,然后你可以开始操作:

import itertools
from functools import reduce

import pandas as pd

data = pd.DataFrame(
    {
        "user_id": [1, 2, 3, 4, 5],
        "sales": [10, 21, 300, 41, 55],
        "example_flag_1": [0, 1, 0, 0, 0],
        "example_flag_2": [1, 1, 1, 1, 1],
        "quartile_1": [1, 2, 3, 4, 1],
        "quartile_2": [1, 2, 3, 4, 1],
    }
)

flag_columns = ["example_flag_1", "example_flag_2", "quartile_1", "quartile_2"]
flag_options = [set(data[col].unique()) for col in flag_columns]
for combo_options in itertools.product(*flag_options):
    combo = {col: option for col, option in zip(flag_columns, combo_options)}
    mask = reduce(lambda x, y: x & y, [data[col] == option for col, option in combo.items()])
    sales_sum = data[mask].sales.sum()
    print(combo, sales_sum)

这将打印出(例如):

{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 1, 'quartile_2': 1} 65
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 1, 'quartile_2': 2} 0
...
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 1} 0
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 2} 0
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 3} 300
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 4} 0
...
英文:

Sure.

Use itertools.product() to generate the combinations, functools.reduce() to generate the mask, and you're off to the races:

import itertools
from functools import reduce

import pandas as pd

data = pd.DataFrame(
    {
        "user_id": [1, 2, 3, 4, 5],
        "sales": [10, 21, 300, 41, 55],
        "example_flag_1": [0, 1, 0, 0, 0],
        "example_flag_2": [1, 1, 1, 1, 1],
        "quartile_1": [1, 2, 3, 4, 1],
        "quartile_2": [1, 2, 3, 4, 1],
    }
)

flag_columns = ["example_flag_1", "example_flag_2", "quartile_1", "quartile_2"]
flag_options = [set(data[col].unique()) for col in flag_columns]
for combo_options in itertools.product(*flag_options):
    combo = {col: option for col, option in zip(flag_columns, combo_options)}
    mask = reduce(lambda x, y: x & y, [data[col] == option for col, option in combo.items()])
    sales_sum = data[mask].sales.sum()
    print(combo, sales_sum)

This prints out (e.g.)

{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 1, 'quartile_2': 1} 65
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 1, 'quartile_2': 2} 0
...
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 1} 0
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 2} 0
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 3} 300
{'example_flag_1': 0, 'example_flag_2': 1, 'quartile_1': 3, 'quartile_2': 4} 0
...

huangapple
  • 本文由 发表于 2023年2月18日 01:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定