Reshaping a pandas dataframe by repeating rows
我是Python的新手,尤其是熊猫。我有一个数据帧
1 2 3 4 5 6 7 | ----------------------------------------------------- Id Name Salary desc1 desc2 desc3 ----------------------------------------------------- 1 ABC1 2000 x1 y1 z1 ----------------------------------------------------- 2 ABC2 5000 x2 y1 z2 ----------------------------------------------------- |
我想把它变成这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ----------------------------------------- ID Name Salary Variable ----------------------------------------- 1 ABC1 2000 x1 ----------------------------------------- 1 ABC1 2000 y1 ----------------------------------------- 1 ABC1 2000 z1 ----------------------------------------- 2 ABC2 5000 x2 ----------------------------------------- 2 ABC2 5000 y2 ----------------------------------------- 2 ABC2 5000 z2 ----------------------------------------- |
请帮忙。事先谢谢。
您需要
1 2 3 4 5 6 7 8 9 10 11 12 13 | df = df.set_index(['Id','Name','Salary']) .stack() .reset_index(level=3, drop=True) .reset_index(name='Variable') print (df) Id Name Salary Variable 0 1 ABC1 2000 x1 1 1 ABC1 2000 y1 2 1 ABC1 2000 z1 3 2 ABC2 5000 x2 4 2 ABC2 5000 y1 5 2 ABC2 5000 z2 |
如果不需要排序前3列:
使用
1 2 3 4 5 6 7 8 9 | df = df.melt(['Id','Name','Salary'], value_name='Variable').drop('variable', axis=1) print (df) Id Name Salary Variable 0 1 ABC1 2000 x1 1 2 ABC2 5000 x2 2 1 ABC1 2000 y1 3 2 ABC2 5000 y1 4 1 ABC1 2000 z1 5 2 ABC2 5000 z2 |
1 2 3 4 5 6 7 8 9 | df = pd.lreshape(df, {'Variable':['desc1','desc2','desc3']}) print (df) Id Name Salary Variable 0 1 ABC1 2000 x1 1 2 ABC2 5000 x2 2 1 ABC1 2000 y1 3 2 ABC2 5000 y1 4 1 ABC1 2000 z1 5 2 ABC2 5000 z2 |