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

丶灬走出姿态 提交于 2019-12-02 01:20:36

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!