【问题标题】:Calculating a sum based on the date from another table根据另一个表中的日期计算总和
【发布时间】:2022-01-20 21:29:55
【问题描述】:

我有以下数据框:

客户发票清单。

Client NetTotal Issued
A 1000 01/01/2021
A 2500 03/01/2021
B 1500 05/01/2021
B 2000 07/01/2021

相同客户的列表和促销优惠的日期。

Client ChangeDate
A 02/01/2021
B 06/01/2021

日期格式为 dd/mm/yyyy。

我需要将它们展平到另一个数据框中,显示更改日期之前和之后的总数,如下所示:

Client ChangeDate NetTotal BeforeChange AfterChange
A 02/01/2021 3500 1000 2500
B 06/01/2021 3500 1500 2000

谢谢。

data_invoices = {'Client': ['A', 'A', 'B', 'B'], 'NetTotal': [1000,2500,1500,2000], 'Issued':['01/01/2021','03/01/2021', '05/01/2021', '07/01/2021']}
df_invoices = pd.DataFrame(data_invoices)

data_changes = {'Client': ['A', 'B'], 'ChangeDate': ['02/01/2021', '06/01/2021']}
df_changes = pd.DataFrame(data_changes)

【问题讨论】:

  • 您可以将数据作为文本发布,以便我们将其粘贴到我们的 IDE 中吗?
  • @gold_cy 添加到 OP 中。
  • 如果两个 DataFrame 中的日期相同,则此行在之前或之后或需要删除?
  • @jezrael 感谢您的反馈,我应该澄清这一点。如果发布日期在更改日期,则应包含在“之后”列中。
  • data_changes = {'Client': ['A', 'B'], 'ChangeDate': ['01/01/2021', '06/01/2021']} 测试,应该可以正常工作

标签: python pandas dataframe date merge


【解决方案1】:

用途:

#convert columns to datetimes
df_invoices['Issued'] = pd.to_datetime(df_invoices['Issued'], dayfirst=True)
df_changes['ChangeDate'] = pd.to_datetime(df_changes['ChangeDate'], dayfirst=True)

#added column for compare with greater
df_invoices['ChangeDate'] = df_invoices['Client'].map(df_changes.set_index('Client')['ChangeDate'])

df_invoices['g'] = np.where(df_invoices['ChangeDate'].gt(df_invoices['Issued']), 'BeforeChange','AfterChange')

#pivoting with aggregate sum
df1 = df_invoices.pivot_table(index='Client', columns='g', values='NetTotal', aggfunc='sum')
#added total aggregation sum with before after column
df = df_changes.join(df_invoices.groupby('Client')['NetTotal'].sum(), on='Client').join(df1, on='Client')
print (df)
  Client ChangeDate  NetTotal  AfterChange  BeforeChange
0      A 2021-01-02      3500         2500          1000
1      B 2021-01-06      3500         2000          1500

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多