【问题标题】:Is there a standard name for the groupby/apply operation?groupby/apply 操作是否有标准名称?
【发布时间】:2021-07-29 18:16:52
【问题描述】:

数据

分类变量 CV1:X1,X2,X3...Xn

数值变量 NV1:1-1000

时间:0-100

C1 Time NV1
X1 0 1
X1 1 100
X2 0 200
X2 1 500

操作 1:按类别变量拆分

数据帧1

C1 Time NV1
X1 0 1
X1 1 100

数据帧2

C1 Time NV1
X2 0 200
X2 1 500

操作2:应用自定义函数

import numpy as np 

def custom_func (anyarray):
    last_element_in_array = anyarray [-1] 
    first_element_in_array = anyarray [0] 
    return  np.sin (last_element_in_array) * np.ln (first_element_in_array)
     
custom_func (DataFrame1 ['NV1'].values) # np.sin (100) * np.ln (1)

操作3:重新组合DataFrame1和DataFrame2

C1 NV1
X2 Custom Function output : np.sin (100) * np.ln (1)
X2 Custom Function output : np.sin (500) * np.ln (200)

【问题讨论】:

标签: pandas parallel-processing apply data-wrangling custom-function


【解决方案1】:

这是groupby 操作:

data.groupby('C1')['NV1'].apply(custom_func)

【讨论】:

  • 感谢您的澄清。此外,应用“自定义函数”和“groupby 操作”的组合是否有自己的名称?!鉴于它是我认为数据转换历史上最常见的案例之一。要添加上下文,我想用谷歌搜索最快的方法。尤其是现在在 python 中有 vaex,Dask,numba,Spark 和所有这些加速包。
猜你喜欢
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
相关资源
最近更新 更多