模拟 excel sumifs / 多标准索引与 python / pandas 中的数据帧匹配
Emulating an excel sumifs / multi-criteria index match with dataframes in python / pandas
我在 python 中有两个数据帧,使用 pandas:
- df1:[233,500 x 6] 按日期分类的价格分类帐
- df2:[1,665,997 x 5] 按日期分类的客户使用分类帐
在 excel 中的较小数据集上,我可以简单地执行以下操作以在 df2 上创建一个包含给定日期的每个产品价格的列,按行。
=SUMIFS(df1[Rate],df1[Date],[@Date],df1[Jurisdiction],[@Jurisdiction],df1[Product],[@Product])
最终我想向 df2 添加一列(或用结果创建一个新的数据框),通过匹配每行中的某些条件来提供价格(管辖权,产品类型)在价格分类帐 df1 中使用相同的标准。
使用 python 和数据帧关联此数据的最合适方法是什么?某种字典和连接?
我找到的例子主要是处理给定一些条件下的求和:
Python Pandas counting and summing specific conditions
奖励:df1 和 df2 中的日期并不总是完全一致。需要将 df2 中的日期与最新的价格分类帐日期相匹配。
编辑:我在下面包含了简化的数据,以演示我如何尝试进行最近的日期匹配。日期匹配只是在 df2 中查找每个日期的活动价格的中间步骤。两个日期列的直接合并不起作用,因为不是每天都提供价格。
df1:
Date Price
1/11/2016 5.00
1/12/2016 5.50
1/13/2016 6.00
1/14/2016 7.00
1/16/2016 8.00
1/20/2016 9.00
1/21/2016 10.00
1/22/2016 11.00
df2:
Date Volume
1/11/2016 100
1/15/2016 100
1/17/2016 200
1/18/2016 300
1/20/2016 200
df3: (df2 with date matching. Cost = Volume*Price)
Date Volume MatchedDate Price Cost
1/11/2016 100 1/11/2016 5.00 500
1/15/2016 100 1/14/2016 7.00 700
1/17/2016 200 1/16/2016 8.00 1600
1/18/2016 300 1/16/2016 8.00 2400
1/20/2016 200 1/20/2016 9.00 1800
编辑 2:以下第一部分中提供的公式在 excel 中起作用,其中包含一些额外的逻辑,用于在第一个 df1 条目之前使用 df2 条目的第一个日期。 https://www.extendoffice.com/documents/excel/2601-excel-find-closest-date.html
{=MAX((df1[Date]<[@Date])*df1[Date])}
只需根据匹配条件合并两组,然后 运行 一个 groupby
总和:
merged_df = pd.merge(df1, df2, on=['date', 'Jurisdiction', 'Product'])
merged_df.groupby(['date', 'Jurisdiction', 'Product'])['Rate'].sum()
或最近日期 df2:
most_recent_df2 = df2.merge(df2.groupby(['Jurisdiction', 'Product'])['date'].max().reset_index(),
on=['date', 'Jurisdiction', 'Product'])
merged_df = pd.merge(df1, most_recent_df2, on=['Jurisdiction', 'Product'], suffixes=['', '_'])
merged_df.groupby(['date', 'Jurisdiction', 'Product'])['Rate'].sum()
我在 python 中有两个数据帧,使用 pandas:
- df1:[233,500 x 6] 按日期分类的价格分类帐
- df2:[1,665,997 x 5] 按日期分类的客户使用分类帐
在 excel 中的较小数据集上,我可以简单地执行以下操作以在 df2 上创建一个包含给定日期的每个产品价格的列,按行。
=SUMIFS(df1[Rate],df1[Date],[@Date],df1[Jurisdiction],[@Jurisdiction],df1[Product],[@Product])
最终我想向 df2 添加一列(或用结果创建一个新的数据框),通过匹配每行中的某些条件来提供价格(管辖权,产品类型)在价格分类帐 df1 中使用相同的标准。
使用 python 和数据帧关联此数据的最合适方法是什么?某种字典和连接?
我找到的例子主要是处理给定一些条件下的求和:
Python Pandas counting and summing specific conditions
奖励:df1 和 df2 中的日期并不总是完全一致。需要将 df2 中的日期与最新的价格分类帐日期相匹配。
编辑:我在下面包含了简化的数据,以演示我如何尝试进行最近的日期匹配。日期匹配只是在 df2 中查找每个日期的活动价格的中间步骤。两个日期列的直接合并不起作用,因为不是每天都提供价格。
df1:
Date Price
1/11/2016 5.00
1/12/2016 5.50
1/13/2016 6.00
1/14/2016 7.00
1/16/2016 8.00
1/20/2016 9.00
1/21/2016 10.00
1/22/2016 11.00
df2:
Date Volume
1/11/2016 100
1/15/2016 100
1/17/2016 200
1/18/2016 300
1/20/2016 200
df3: (df2 with date matching. Cost = Volume*Price)
Date Volume MatchedDate Price Cost
1/11/2016 100 1/11/2016 5.00 500
1/15/2016 100 1/14/2016 7.00 700
1/17/2016 200 1/16/2016 8.00 1600
1/18/2016 300 1/16/2016 8.00 2400
1/20/2016 200 1/20/2016 9.00 1800
编辑 2:以下第一部分中提供的公式在 excel 中起作用,其中包含一些额外的逻辑,用于在第一个 df1 条目之前使用 df2 条目的第一个日期。 https://www.extendoffice.com/documents/excel/2601-excel-find-closest-date.html
{=MAX((df1[Date]<[@Date])*df1[Date])}
只需根据匹配条件合并两组,然后 运行 一个 groupby
总和:
merged_df = pd.merge(df1, df2, on=['date', 'Jurisdiction', 'Product'])
merged_df.groupby(['date', 'Jurisdiction', 'Product'])['Rate'].sum()
或最近日期 df2:
most_recent_df2 = df2.merge(df2.groupby(['Jurisdiction', 'Product'])['date'].max().reset_index(),
on=['date', 'Jurisdiction', 'Product'])
merged_df = pd.merge(df1, most_recent_df2, on=['Jurisdiction', 'Product'], suffixes=['', '_'])
merged_df.groupby(['date', 'Jurisdiction', 'Product'])['Rate'].sum()