如何使用熊猫在一行中转置多索引数据

时间:2019-11-01 11:30:28

标签: python pandas

我有下面的系列。

df1
   Product
M1 Product1
   Product2
M2 Product1
   Product3
M3 Product1
   Product4

大熊猫中是否有任何资源可以在一行中的多索引内转置数据?如下面的示例。

df1

   Product  ProducNew
M1 Product1 Product2
M2 Product1 Product3
M3 Product1 Product4

2 个答案:

答案 0 :(得分:0)

按索引分组,并创建两个具有第一个值和最后一个值的列

df.groupby(df.index)['Product'].agg(product='first', productNew='last')

     product productNew
M1  Product1   Product2
M2  Product1   Product3
M3  Product1   Product4

答案 1 :(得分:0)

您建议此代码返回下面的df。

    0
0  Product1
1  Product2
2  Product1
3  Product3
4  Product1
5  Product4...

对不起,我不是初学者。