Expanding pandas Data Frame rows based on number and group ID (Python 3).

前端 未结 1 1629
刺人心
刺人心 2021-01-22 03:54

I have been struggling with finding a way to expand/clone observation rows based on a pre-determined number and a grouping variable (id). For context, here is an example data f

1条回答
  •  面向向阳花
    2021-01-22 04:38

    I can't replicate your index, but I can replicate your values, using np.repeat, quite easily in fact.

    v = df.values
    df = pd.DataFrame(v.repeat(v[:, -1], axis=0), columns=df.columns)
    

    If you want the exact index (although I can't see why you'd need to), you'd need a groupby operation -

    def f(x):
        return x.astype(str) + '.' + np.arange(len(x)).astype(str)
    
    idx = df.groupby('id').id.apply(f).values
    

    Assign idx to df's index -

    df.index = idx
    

    0 讨论(0)
提交回复
热议问题