在pandas DataFrame中分组和聚合数据

nlejzf6q  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(179)

我有一个包含事务数据的Pandas DataFrame,我想执行分组和聚合操作来分析不同级别的数据。我已经尝试过使用groupby和agg函数,但在达到预期结果时遇到了一些困难。
下面是DataFrame结构和数据的示例:

import pandas as pd

data = {
    'Product': ['A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'A', 'D'],
    'Transaction_ID': ['id1', 'id1', 'id2', 'id3', 'id3', 'id3', 'id4', 'id4', 'id4', 'id5','id6'],
    'Size': ['S', 'M', 'L', 'S', 'M', 'L', 'S', 'S', 'M', 'S','M'],
    'Demand_Qty': [5, 3, 2, 2, 1,2, 1, 4, 1, 1,1]
}

df1 = pd.DataFrame(data)

我想执行以下操作:
1.检查每笔交易中是否有多个大小
1.检查是否存在大小相同但数量多的交易
1.总事务计数
我尝试使用groupby和agg函数,但没有得到所需的输出。以下是我尝试过的代码:

product_order_grouped = df1.groupby(['Product', 'Transaction_ID']).agg(
    multiple_sizes_in_transaction=('Size', lambda s: s.nunique() > 1),
    same_sizes_in_transaction=('Size', lambda s: s.nunique() == 1 and df1.loc[s.index, 'Demand_Qty'] > 1)
).reset_index()

product_grouped = product_order_grouped.groupby('Product').agg(
    Total_Transactions=('Transaction_ID', 'count'),
    Transactions_with_Multiple_Sizes=('multiple_sizes_in_transaction', 'sum'),
    Transactions_with_Same_Size_and_Multiple_Quantities=('same_sizes_in_transaction', 'sum'),
).reset_index()

print(product_grouped)

我得到的输出不是预期的那样。有人能指导我如何正确地对DataFrame执行这些分组和聚合操作以获得所需的结果吗?

当前输出

Product  Total_Transactions  Transactions_with_Multiple_Sizes  Transactions_with_Same_Size_and_Multiple_Quantities
0       A                   3                                1                                                 1
1       B                   2                                1                                                 0
2       C                   1                                1                                                 0
3       D                   1                                0                                                 0

预期输出

Product  Total_Transactions  Transactions_with_Multiple_Sizes  Transactions_with_Same_Size_and_Multiple_Quantities
0       A                   3                                1                                                 2
1       B                   2                                1                                                 1
2       C                   1                                1                                                 1
3       D                   1                                0                                                 0

获取所需结果的逻辑Transactions_with_Same_Size_and_Multiple_Quantities

Product Transaction_ID Size  Demand_Qty
0        A            id1    S           5
1        A            id1    M           3
2        A            id2    L           2
3        B            id3    S           2
4        B            id3    M           1
5        B            id3    L           2
6        B            id4    S           1
7        C            id4    S           4
8        C            id4    M           1
9        A            id5    S           1
10       D            id6    M           1

如果我们只看产品A

Product Transaction_ID Size  Demand_Qty
0        A            id1    S           5
1        A            id1    M           3
2        A            id2    L           2
9        A            id5    S           1
  • 那么id 1和id 2是2个交易,其中需求量大于1,因此值应该是2*
  • 类似地,对于产品B & C,该值应为1,因为只有1个Transaction_ID在需求量中具有多于1个的值,对于& D,该值应为0*

我将非常感谢有关如何在DataFrame上正确执行这些分组和聚合操作以获得所需结果的任何指导或建议。

P.S. -如果有人可以建议我查看任何其他可以分享有关此数据的更好见解的指标,我也持开放态度||因为例如,如果我们再看看产品A,实际上有3个示例的需求大于1,所以我不确定我的指标是否足以分析数据

添加更多测试数据

import pandas as pd

data = {
    'Transaction_ID': [1357778791, 1357779263, 1357779570, 1357779583, 1357779893, 1357779893, 1357782347, 1357782681, 1357782681, 1357783510, 1357784048, 1357784401, 1357784564, 1357784564, 1357784670, 1357784816, 1357784816, 1357785798, 1357786529, 1357787012, 1357787208, 1357787837, 1357788325, 1357788326, 1357788452, 1357788542, 1357788585, 1357788585, 1357789168, 1357789633, 1357789633, 1357790352, 1357790366, 1357790379, 1357790730, 1357792699, 1357794652, 1357795141, 1357795141, 1357795147, 1357795805, 1357796833, 1357797368, 1357797714, 1357797789, 1357798619, 1357799260, 1357799933, 1357802692, 1357802692, 1357802771, 1357802818, 1357803663, 1357804255, 1357804868, 1357805887, 1357805941, 1357807095, 1357807122, 1357807122, 1357807897, 1357808324, 1357808324],
     'Product': [2199692] * 63,
    'Size': [48, 46, 36, 44, 44, 42, 36, 38, 36, 48, 36, 36, 44, 42, 38, 40, 38, 46, 36, 36, 40, 40, 36, 44, 48, 42, 44, 42, 42, 46, 44, 36, 48, 40, 36, 48, 38, 46, 44, 38, 46, 40, 36, 36, 36, 36, 44, 48, 42, 44, 38, 38, 38, 48, 48, 46, 40, 38, 44, 40, 40, 40, 38],
    'Demand_Qty': [1] * 63
    }

df1 = pd.DataFrame(data)

# Print the DataFrame
print(df1)
vxqlmq5t

vxqlmq5t1#

我希望我已经很好地理解了你的问题。您可以尝试:

def fn(x):
    total_transactions = x["Transaction_ID"].nunique()

    transactions_with_multiple_sizes = (
        x.groupby(x["Transaction_ID"])["Size"].nunique() > 1
    ).sum()

    transactions_with_same_size_and_multiple_quantities = (
        x.groupby("Transaction_ID")["Demand_Qty"].sum() > 1
    ).sum()

    return pd.Series(
        {
            "Total_Transactions": total_transactions,
            "Transactions_with_Multiple_Sizes": int(transactions_with_multiple_sizes),
            "Transactions_with_Same_Size_and_Multiple_Quantities": transactions_with_same_size_and_multiple_quantities,
        }
    )

product_grouped = df1.groupby("Product").apply(fn)
print(product_grouped)

图纸:

Total_Transactions  Transactions_with_Multiple_Sizes  Transactions_with_Same_Size_and_Multiple_Quantities
Product                                                                                                           
A                         3                                 1                                                    2
B                         2                                 1                                                    1
C                         1                                 1                                                    1
D                         1                                 0                                                    0

初始df

Product Transaction_ID Size  Demand_Qty
0        A            id1    S           5
1        A            id1    M           3
2        A            id2    L           2
3        B            id3    S           2
4        B            id3    M           1
5        B            id3    L           2
6        B            id4    S           1
7        C            id4    S           4
8        C            id4    M           1
9        A            id5    S           1
10       D            id6    M           1

相关问题