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
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