python pandas merge_asof groupby

标签 python pandas dataframe merge

我有一个合并的数据框,如下所示:

>>> merged_df.dtypes
Jurisdiction                  object
AdjustedVolume               float64
EffectiveStartDate    datetime64[ns]
VintageYear                    int64
ProductType                   object
Rate                         float32
Obligation                   float32
Demand                       float64
Cost                         float64
dtype: object

以下 groupby 语句按管辖区/年份返回正确的调整卷值:

>>> merged_df.groupby(['Jurisdiction', 'VintageYear'])['AdjustedVolume'].sum()

当包含产品类型时:

>>> merged_df.groupby(['Jurisdiction', 'VintageYear','ProductType'])['AdjustedVolume'].sum()

如果管辖区仅包含一种产品类型,则按年份调整的销量是正确​​的,但对于具有两种或多种产品类型的任何管辖区,调整后的销量将被拆分,以便它们总和为正确的值。我期望每一行都有总的调整卷,但不清楚为什么要分割它。

示例:

>>> merged_df.groupby(['Jurisdiction', 'VintageYear'])['AdjustedVolume'].sum()
Jurisdiction  VintageYear  AdjustedVolume
CA            2017         3.529964e+05


>>> merged_df.groupby(['Jurisdiction', 'VintageYear','ProductType'])['AdjustedVolume'].sum()
Jurisdiction  VintageYear  ProductType  AdjustedVolume
CA            2017         Bucket1      7.584832e+04
CA            2017         Bucket2      1.308454e+05
CA            2017         Bucket3      1.463026e+05

我怀疑 merge_asof 执行不正确:

>>> df1.dtypes
Jurisdiction                  object
ProductType                   object
VintageYear                    int64
EffectiveStartDate    datetime64[ns]
Rate                         float32
Obligation                   float32
dtype: object
>>> df2.dtypes
Jurisdiction                  object
AdjustedVolume               float64
EffectiveStartDate    datetime64[ns]
VintageYear                    int64
dtype: object

由于 df2 没有 ProductType 字段,因此以下合并将总数量分解为每个管辖区下的任何 ProductType。我可以修改以下合并,以便每个 ProductType 都有总的 AdjustedVolume 吗?

merged_df = pd.merge_asof(df2, df1, on='EffectiveStartDate', by=['Jurisdiction','VintageYear'])

最佳答案

您可以使用两个版本的分组依据并合并两个表。 第一个表是一个包含 ProductType 的分组,它将按 ProductType 分割您的 AdjustedVolume。

df = df.groupby(['Jurisdiction','VintageYear','ProductType']).agg({'AdjustedVolume':'sum'}).reset_index(drop = False)

然后创建另一个表,不包含 ProductType(这是总金额的来源)。

df1 = df.groupby(['Jurisdiction','VintageYear']).agg({'AdjustedVolume':'sum'}).reset_index(drop = False)

现在在两个表中创建一个 ID 列,以便合并正常工作。

df['ID'] = df['Jurisdiction'].astype(str)+'_' +df['VintageYear'].astype(str)
df1['ID'] = df1['Jurisdiction'].astype(str)+'_'+ df1['VintageYear'].astype(str)

现在合并 ID 以获得调整后的总音量。

df = pd.merge(df, df1, left_on = ['ID'], right_on = ['ID'], how = 'inner')

最后一步是清理您的列。

df = df.rename(columns = {'AdjustedVolume_x':'AdjustedVolume',
                          'AdjustedVolume_y':'TotalAdjustedVolume',
                          'Jurisdiction_x':'Jurisdiction',
                          'VintageYear_x':'VintageYear'})
del df['Jurisdiction_y']
del df['VintageYear_y']

您的输出将如下所示:

enter image description here

关于python pandas merge_asof groupby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447812/

相关文章:

python - 井字棋 python 中的简单帮助

python - Pandas - 对于每个索引,将所有列放入行中

python - 如何根据一列的字符串相似度链接两个数据框

python - 行选择结合了 pandas 数据框中索引的条件和列的条件

Python - 初始化多个列表/行

python - 如何最好地将数据流式传输到 IP 地址?

Python Pandas - 使用条件填充新行

python - 当时间戳作为Python中的索引时如何删除特定行

python - seaborn可以画断 Axis 图吗?

python-2.7 - 将多个 csvs 读入 Pandas 中的多个数据帧