Pandas - 在 groupby 后将列转换为新行

6

我有一个pandas数据帧。我需要将其中的一些列转换为行。对于每三行,数据帧的前两列中都有相同的数据。因此,正如您在我的期望数据框中所看到的那样,我需要6个更多的列。

我有以下数据帧:

shopCode    Product   Code  Score
    111      Apple    123    0.70
    111      Apple    456    0.75
    111      Apple    789    0.80
    222      Orange   142    0.66
    222      Orange   136    0.83
    222      Orange   623    0.76

我期望的数据框如下:

shopCode  Product   Code1 Code2 Code3 Score1 Score2 Score3
  111      Apple     123   456   789   0.70   0.75   0.80
  222      Orange    142   136   623   0.66   0.83   0.76

我尝试使用df.pivot(index=['店铺代码', '产品'], columns=['码1', '码2', '码3', '分数1', '分数2', '分数3'], values=['码', '分数']),但是不起作用。

2个回答

12
我们正在使用 pivot_table
df=pd.pivot_table(df,index=['shopCode','Product'],columns=df.groupby(['shopCode','Product']).cumcount().add(1),values=['Code','Score'],aggfunc='sum')
df.columns=df.columns.map('{0[0]}{0[1]}'.format) 
df
Out[112]: 
                  Code1  Code2  Code3  Score1  Score2  Score3
shopCode Product                                             
111      Apple      123    456    789    0.70    0.75    0.80
222      Orange     142    136    623    0.66    0.83    0.76

使用 reset_index

df.reset_index()
Out[113]: 
   shopCode Product  Code1  Code2  Code3  Score1  Score2  Score3
0       111   Apple    123    456    789    0.70    0.75    0.80
1       222  Orange    142    136    623    0.66    0.83    0.76

2
可以使用pandas的groupby和字典合并来实现。
df.groupby(['shopCode', 'Product']).apply(lambda x: pd.Series(
                               {
                                **{'Code'+str(i+1): t for i,t in enumerate(x.Code)},
                                **{'Score'+str(i+1): t for i,t in enumerate(x.Score)}
                               }
                             )).reset_index()

用那段代码,你不会得到行作为列! - FatiHe
它需要使用unstack()函数将行转换为列。...unstack().reset_index() - FatiHe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接