pandas:标准化组内的值,每组一个引用值(groupby?拆分-应用-组合?)

标签 pandas

我有一组测量值;每个测量值都是 DataFrame 中的一行。我想在这些测量中添加一列,以反射(reflect)该测量与引用相比的加速情况。每个测量都通过其“数据集”和“算法”来区分,并且每个数据集-算法对都有一个引用运行时。

col = ['program', 'dataset', 'algorithm', 'extra', 'runtime']
df = pandas.DataFrame(
    [['program-ref', 'dataset-X', 'algorithm-i', 'x', 1.0],
     ['program-ref', 'dataset-X', 'algorithm-j', 'x', 2.0],
     ['program-ref', 'dataset-Y', 'algorithm-i', 'x', 3.0],
     ['program-ref', 'dataset-Y', 'algorithm-j', 'x', 4.0],
     ['program-B', 'dataset-X', 'algorithm-i', 'x', 5.0],
     ['program-B', 'dataset-X', 'algorithm-j', 'x', 6.0],
     ['program-B', 'dataset-Y', 'algorithm-i', 'x', 7.0],
     ['program-B', 'dataset-Y', 'algorithm-j', 'x', 8.0],
     ['program-C', 'dataset-X', 'algorithm-i', 'x', 9.0],
     ['program-D', 'dataset-X', 'algorithm-j', 'x', 10.0],
     ['program-E', 'dataset-Y', 'algorithm-i', 'x', 11.0],
     ['program-E', 'dataset-Y', 'algorithm-j', 'x', 12.0],
    ], columns=col)

我想添加一个名为“加速”的列,其中每个测量的“加速”计算为测量的运行时间除以引用测量的运行时间(对于该数据集-算法对) )。例如,在上面的 DataFrame 中,第 5 行(程序 B、数据集 X、算法 i)的“加速比”应为 1/(5.0/1.0)。

这似乎是 split-apply-combine ( http://pandas.pydata.org/pandas-docs/stable/groupby.html ) 的一个实例,但显示的 apply 函数通常是组中所有内容的聚合或其输入只是一个特定测量的函数。在这里,我需要将引用测量“应用”到其组中的所有内容。

我还在上面添加了“额外”列,因为我希望输出与输入相同(除了新的“加速”列之外),而 groupby 似乎想要剔除所有“麻烦”列。

最佳答案

我不喜欢为实现目标而设置数据,因为每个算法-数据集组合有多个程序名称。另请注意,鉴于您的示例数据,groupby 方法是无关的,因为程序-数据集-算法值存在独特的组合。也许您的真实数据有不同的要求?如果是这样,请更新示例数据以反射(reflect)要求。与此同时,请尝试以下操作。

将引用值与其余数据合并起来会更容易,以便适当的值可以更轻松地相互关联。

ref_df = df.loc[df['program'] == 'program-ref', ['dataset', 'algorithm', 'runtime']]
# EDIT: only include the following line if you wish to remove the reference
# rows from the final output
# df = df.loc[~(df['program'] == 'program-ref')]

new_df = pd.merge(df, ref_df, on=['dataset', 'algorithm'],
                              suffixes=['', '_ref'])

# you don't actually need a groupby since there are unique 
# program-dataset-algorithm combinations.
new_df['speedup'] = 1/(new_df['runtime']/new_df['runtime_ref'])

# optional groupby approach
new_df['speedup'] = new_df.groupby(['program', 'dataset', 'algorithm']).apply(
                           lambda x: 1/(x['runtime']/x['runtime_ref'])).values

>>> new_df.sort_values('program', ascending=False)
        program    dataset    algorithm extra  runtime  runtime_ref   speedup
0   program-ref  dataset-X  algorithm-i     x      1.0          1.0  1.000000
3   program-ref  dataset-X  algorithm-j     x      2.0          2.0  1.000000
6   program-ref  dataset-Y  algorithm-i     x      3.0          3.0  1.000000
9   program-ref  dataset-Y  algorithm-j     x      4.0          4.0  1.000000
8     program-E  dataset-Y  algorithm-i     x     11.0          3.0  0.272727
11    program-E  dataset-Y  algorithm-j     x     12.0          4.0  0.333333
5     program-D  dataset-X  algorithm-j     x     10.0          2.0  0.200000
2     program-C  dataset-X  algorithm-i     x      9.0          1.0  0.111111
1     program-B  dataset-X  algorithm-i     x      5.0          1.0  0.200000
4     program-B  dataset-X  algorithm-j     x      6.0          2.0  0.333333
7     program-B  dataset-Y  algorithm-i     x      7.0          3.0  0.428571
10    program-B  dataset-Y  algorithm-j     x      8.0          4.0  0.500000

关于pandas:标准化组内的值,每组一个引用值(groupby?拆分-应用-组合?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517420/

相关文章:

python - 通过字符串变量 reshape 数据框

python-3.x - 如何在每组pandas groupby对象中添加标志列

python - 识别 Pandas DataFrame 中的前导和尾随 NA

Python - 分类的最低方差的箱大小

python - 按列对数据框进行分组并保存到不同的目录

python - 如何在不组合行级别的情况下使用 Pandas 进行热编码

python - 如果列不存在并出现 KeyError : "null"Column'] not in index"in df. to_csv,请写入 "['?

Python Pandas to_dict 函数

Python Pandas,将表格更改为百分比而不是单个结果

python - 将字符串替换为 DataFrame 列中的子字符串