Pandas Pivot表 - 在多索引表中添加小计

发布于 2025-01-31 18:19:35 字数 1210 浏览 1 评论 0原文

我有一张构造的数据表:

名字付款ID约翰
·杜伊T077731263754
John DoeT077132313134
Jane DoeS044123132113
John DoeJ544 463444463456453

我想实现的输出,我想实现一个pivot桌子类似格式:

姓名交易总和
约翰·杜(John Doe)3141
--- T077288
--- J544153
JANE DOE113
---------- S044113

请记住:

  • 付款ID唯一标识交易(每条线中的每一行)表)
  • 每个名称都可以使用我使用pandas pivot_table尝试的一张或多张卡的一项或多件交易

,但是我找不到按照我想要的构造数据的方法(包括每个名称的小计),我只能按名称和卡片进行分组 。

pd.pivot_table(df, values='Amount', index=['Name','Card'], aggfunc=(np.sum, len))

对桌子上的格式不佳,对不起,我的降价技能非常有限

有什么帮助吗?

I have a table of data structured as it follows:

NameCardPayment IDAmount
John Doet077731263754
John Doet077132313134
Jane Does044123132113
John Doej544463456453

The output I want to achieve is to have a pivot table with a similar format:

NameNumber of TransactionsSum
John Doe3141
--- t077288
--- j544153
Jane Doe113
--- s044113

Please keep in mind that:

  • Payment ID uniquely identifies the transaction (every line in the table)
  • Every Name can have one or multiple transactions with one or multiple cards

I tried using pandas pivot_table, however I cannot find a way to structure the data as I want (including subtotals per Name), I can only group by Name and Card using

pd.pivot_table(df, values='Amount', index=['Name','Card'], aggfunc=(np.sum, len))

Sorry for the poor formatting on the table, my markdown skills are quite limited.

Any help on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2025-02-07 18:19:35

枢轴表是一种很好的方法,尝试:

table = pd.pivot_table(
    df, 
    values=['Amount'],
    index=['Name', 'Card'],
    aggfunc=['count', 'sum'],
)

# Adds subtotals, and sorts:
pd.concat([
    d.append(d.sum().rename((k, 'Total')))
    for k, d in table.groupby(level=0)
]).sort_index(ascending=[False, True])

输出:

                count    sum
               Amount Amount
Name     Card
Joe Doe  Total      3    141
         j544       1     53
         t077       2     88
Jane Doe Total      1     13
         s044       1     13

次数参考: link。

Pivot table is a good approach, try:

table = pd.pivot_table(
    df, 
    values=['Amount'],
    index=['Name', 'Card'],
    aggfunc=['count', 'sum'],
)

# Adds subtotals, and sorts:
pd.concat([
    d.append(d.sum().rename((k, 'Total')))
    for k, d in table.groupby(level=0)
]).sort_index(ascending=[False, True])

Output:

                count    sum
               Amount Amount
Name     Card
Joe Doe  Total      3    141
         j544       1     53
         t077       2     88
Jane Doe Total      1     13
         s044       1     13

Subtotal reference: link.

小ぇ时光︴ 2025-02-07 18:19:35

请确保使用pivot_table使用margins = true,然后使用以下功能:

def pivot_table_w_subtotals(df, values, indices, columns, aggfunc, fill_value):
    '''
    Adds tabulated subtotals to pandas pivot tables with multiple hierarchical indices.
    
    Args:
    - df - dataframe used in pivot table
    - values - values used to aggregrate
    - indices - ordered list of indices to aggregrate by
    - columns - columns to aggregrate by
    - aggfunc - function used to aggregrate (np.max, np.mean, np.sum, etc)
    - fill_value - value used to in place of empty cells
    
    Returns:
    -flat table with data aggregrated and tabulated
    
    '''
    listOfTable = []
    for indexNumber in range(len(indices)):
        n = indexNumber+1
        if n == 1:
            table = pd.pivot_table(df,values=values,index=indices[:n],columns=columns,aggfunc=aggfunc,fill_value=fill_value,margins=True,observed=True)
        else:
            table = pd.pivot_table(df,values=values,index=indices[:n],columns=columns,aggfunc=aggfunc,fill_value=fill_value,observed=True)
        table = table.reset_index()
        for column in indices[n:]:
            table[column] = ''
        listOfTable.append(table)
    concatTable = pd.concat(listOfTable).sort_index()
    concatTable = concatTable.set_index(keys=indices)
    return concatTable.sort_index(axis=0,ascending=True)

使用示例:

pivot_table_w_subtotals(df=df_pivot,values=['Value1','Value2'],indices=['Store','Department','Type'],columns=[],aggfunc='sum',fill_value='')

注意:我将以下内容添加到两个>上的代码中table = lines :(

,observed=True

我正在修改它的过程,因为我有一个dcategorical排序,该排序位于原始df_pivot中,但在返回的表中没有。任何帮助都很棒!

Be sure to create a pivot_table with margins=True and then use the function below:

def pivot_table_w_subtotals(df, values, indices, columns, aggfunc, fill_value):
    '''
    Adds tabulated subtotals to pandas pivot tables with multiple hierarchical indices.
    
    Args:
    - df - dataframe used in pivot table
    - values - values used to aggregrate
    - indices - ordered list of indices to aggregrate by
    - columns - columns to aggregrate by
    - aggfunc - function used to aggregrate (np.max, np.mean, np.sum, etc)
    - fill_value - value used to in place of empty cells
    
    Returns:
    -flat table with data aggregrated and tabulated
    
    '''
    listOfTable = []
    for indexNumber in range(len(indices)):
        n = indexNumber+1
        if n == 1:
            table = pd.pivot_table(df,values=values,index=indices[:n],columns=columns,aggfunc=aggfunc,fill_value=fill_value,margins=True,observed=True)
        else:
            table = pd.pivot_table(df,values=values,index=indices[:n],columns=columns,aggfunc=aggfunc,fill_value=fill_value,observed=True)
        table = table.reset_index()
        for column in indices[n:]:
            table[column] = ''
        listOfTable.append(table)
    concatTable = pd.concat(listOfTable).sort_index()
    concatTable = concatTable.set_index(keys=indices)
    return concatTable.sort_index(axis=0,ascending=True)

Use example:

pivot_table_w_subtotals(df=df_pivot,values=['Value1','Value2'],indices=['Store','Department','Type'],columns=[],aggfunc='sum',fill_value='')

Note: I added the following into the code on both of the table = lines:

,observed=True

(source)

I am in the process of modifying this a little bit, as I have a dCategorical sort that is present in the original df_pivot but not in the returned table. Any help would be great!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文