- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个合并的数据框,如下所示:
>>> 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']
您的输出将如下所示:
关于python pandas merge_asof groupby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447812/
我知道以前也有人问过类似的问题。但是,我的问题略有不同。我正在尝试跨多个列获取 merge_asof 的功能。这是数据集: import pandas as pd left = pd.DataFram
我有一个合并的数据框,如下所示: >>> merged_df.dtypes Jurisdiction object AdjustedVolume
我有两个数据框: DF1: StartDate Location 2013-01-01 20000002 2013-03-01 20000002 2013-08-01
我正在尝试按时间合并多个匹配的两个数据帧。我正在查找 timestamp 在 df1 中的 endofweek 之前 7 天或更短时间的 df2 的所有实例。可能有多个记录符合这种情况,我想要所有匹配
我正在尝试使用 pd.merge_asof 合并多个数据帧。 它们都包含 2 个以日期时间作为索引列的列和一个具有 float 值的变量列。它们的索引和时间不平衡,因此我必须对值进行标准化。 Date
嗨,我需要将一些时间序列数据与最近的时间戳对齐,所以我认为 pandas.merge_asof可能是一个很好的候选人。但是,它没有设置 how='outer' 的选项。就像在标准中 merge方法。
我有以下 2 个 DataFrame。 A 具有数据,B 具有数据的权重。 B 具有权重作为索引生效的日期,“level_1”具有与权重相关的实体。 A = pd.DataFrame(index=pd
我尝试使用 merge_asof 加入 2 个数据帧功能。第一个数据框包含一行失败的组件。 >>> import pandas as pd >>> from datetime import datet
我是第一次使用 Azure Data Lake Analytics,我不确定如何像在 python 中使用 pandas 那样合并 2 个数据集。 我正在合并两个具有不同时间戳的数据集,但如果它们在特
我正在处理两个数据集,每个数据集都有不同的关联日期。我想合并它们,但由于日期不完全匹配,我相信 merge_asof() 是最好的方法。 但是,merge_asof() 会发生两件不理想的事情: 数字
我有一个数据集,包括轮类的开始和结束时间,如下所示: schedule = pd.DataFrame({ "start": pd.to_datetime(['2017-01-01 00:59:
代码是: import numpy as np import pandas as pd dateparse = lambda x: pd.datetime.strptime(x,'%d %m %Y %
我发现了这个不错的函数 pandas.merge_asof。来自文档 pandas.merge_asof(left, right, on=None, left_on=None, right_on=No
表 A 有很多列,其中有一个日期列,表 B 有一个日期时间和一个值。两个表中的数据都是零星生成的,没有固定的时间间隔。表 A 很小,表 B 很大。 我需要在 A.datetime 的给定元素 a 的条
当使用 pandas merge_asof 时,如下例所示 import pandas as pd left = pd.DataFrame({'a': [1.1, 5.5, 10.9], 'left_
我有两个数据帧,df1 和 df2 说,它们都是多索引的。 在第一个索引级别,两个数据帧共享相同的键(即 df1.index.get_level_values(0) 和 df2.index.get_l
我想将目标列从一个数据框合并到另一个数据框。合并后的数据框有更多的键,它们很接近,但与原始数据框不完全匹配。请参见下面的示例: 原始数据帧(FDMA) DMA 130506 130510 1305
pandas merge_asof 函数 documentation page给出了一个对我来说效果很好的例子: left = pd.DataFrame({'a': [1, 5, 10], 'left
我刚刚发现了 pandas merge_asof 函数并试图让它工作。 下面是一些重现我所看到的错误的示例代码: d1 = pd.DataFrame.from_dict({'id':[646327.0
pandas.merge_asof 合并两个数据帧,执行左连接,除非它匹配最近的键而不是相等的键。 示例(从 documentation 窃取): >>> quotes
我是一名优秀的程序员,十分优秀!