Python Groupby函数向量化

时间:2018-11-07 23:29:08

标签: python pandas numpy

我发布了上一个问题(Python Groupby with Boolean Mask),该问题提供了成功的答案:

import io
import pandas as pd

data = """ 
id,atr1,atr2,orig_date,fix_date
1,bolt,l,2000-01-01,nan
1,screw,l,2000-01-01,nan
1,stem,l,2000-01-01,nan
2,stem,l,2000-01-01,nan
2,screw,l,2000-01-01,nan
2,stem,l,2001-01-01,2001-01-01
3,bolt,r,2000-01-01,nan
3,stem,r,2000-01-01,nan
3,bolt,r,2001-01-01,2001-01-01
3,stem,r,2001-01-01,2001-01-01
"""
data = io.StringIO(data)
df = pd.read_csv(data, parse_dates=['orig_date', 'fix_date'])

def f(g):
    min_fix_date = g['fix_date'].min()
    if pd.isnull(min_fix_date):
        g['failed_part_ind'] = 0
    else:
        g['failed_part_ind'] = g['orig_date'].apply(lambda d: 1 if d < min_fix_date else 0)
    return g

df.groupby(['id', 'atr1', 'atr2']).apply(lambda g: f(g))

结果如下:

id,atr1,atr2,orig_date,fix_date,failed_part_ind
1,bolt,l,2000-01-01,nan,0
1,screw,l,2000-01-01,nan,0
1,stem,l,2000-01-01,nan,0
2,stem,l,2000-01-01,nan,1
2,screw,l,2000-01-01,nan,0
2,stem,l,2001-01-01,2001-01-01,0
3,bolt,r,2000-01-01,nan,1
3,stem,r,2000-01-01,nan,1
3,bolt,r,2001-01-01,2001-01-01,0
3,stem,r,2001-01-01,2001-01-01,0

但是,我现在正在尝试开发优化/矢量化的版本,以改善运行时间并扩展到更大的数据集。任何提示或技巧将是最欢迎的!我目前正在尝试使用熊猫.idxmin()和numpy .argmin()

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

df.groupby(['id','atr1','atr2']).apply(lambda x: (x.orig_date < pd.to_datetime(x.fix_date.min()))
                                .astype(int)).reset_index()