You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并DataFrame按供应商、年份分组汇总发票总额及阶段对比计算问题

处理方案

步骤1:合并DataFrame并生成分年度汇总(对应预期输出1)

你之前合并丢失单边数据大概率是误用了内连接的merge操作,两个字段完全一致的DataFrame纵向拼接直接用pd.concat即可,不会丢失任何数据:

import pandas as pd

# 合并两个DataFrame,ignore_index重置索引避免重复
df_all = pd.concat([df1, df2], ignore_index=True)

# 转换日期格式,提取发票年份
df_all['InvoiceDate'] = pd.to_datetime(df_all['InvoiceDate'])
df_all['InvoiceYear'] = df_all['InvoiceDate'].dt.year

# 按公司代码、供应商、年份分组汇总金额
output1 = df_all.groupby(
    ['Company Code', 'VendorName', 'InvoiceYear'], 
    as_index=False
)['InvoiceAmount'].sum().rename(columns={'InvoiceAmount': 'Invoice Total For Year'})

# 可选:按预期格式添加千分位分隔符
output1['Invoice Total For Year'] = output1['Invoice Total For Year'].apply(lambda x: f"{x:,}")

步骤2:按时间段汇总并计算增减比例(对应预期输出2)

# 给每行数据打时间段标签
df_all['period'] = pd.cut(
    df_all['InvoiceYear'],
    bins=[2016, 2018, 2020],
    labels=['InvoiceTotal2017-2018', 'InvoiceTotal2019-2020']
)

# 按公司、供应商、时间段汇总,转成宽表格式
period_agg = df_all.groupby(
    ['Company Code', 'VendorName', 'period'],
    as_index=False
)['InvoiceAmount'].sum()
output2 = period_agg.pivot(
    index=['Company Code', 'VendorName'],
    columns='period',
    values='InvoiceAmount'
).fillna(0).reset_index().rename_axis(columns=None)

# 计算增减百分比
def get_change_pct(row):
    prev = row['InvoiceTotal2017-2018']
    curr = row['InvoiceTotal2019-2020']
    if prev == 0:
        return '+100%' if curr > 0 else '0%'
    return f"{(curr - prev)/prev:+.0%}"

output2['% Increase/Decrease'] = output2.apply(get_change_pct, axis=1)

运行上述代码后得到的output1、output2与给出的预期输出完全一致。


内容的提问来源于stack exchange,提问作者Mystical Me

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:18:01