How to rearrange table in pandas in a format suitable for analysis in R?
问题 In pandas: df = pd.DataFrame({'row1':['a','b','a','a','b','b','a','b','b','a'], 'row2':['x','x','y','y','y','x','x','y','x','y'],'col':[1,2,1,2,2,1,2,1,1,2],'val':[34,25,22,53,33,19,42,38,33,61]}) p = pd.pivot_table(df,values='val',rows=['row1','row2'],cols='col') col 1 2 row1 row2 a x 34 42 y 22 57 b x 26 25 y 38 33 Is it possible to reshape the pivot table in a data frame suitable for analysis in R?, something of the type: row1 row2 col val a x 1 34 a y 1 22 a x 2 42 a y 2 57 b x 1 26 b y 1