Pandas:将具有重复行名的数据重新整形为列

时间:2012-10-18 14:55:32

标签: python pandas

我有一个类似这样的数据集(显示的第一行):

Sample  Detector        Cq
P_1   106    23.53152
P_1   106    23.152458
P_1   106    23.685083
P_1   135        24.465698
P_1   135        23.86892
P_1   135        23.723469
P_1   17  22.524242
P_1   17  20.658733
P_1   17  21.146122

“Sample”和“Detector”列都包含重复值(“Cq”是唯一的):准确地说,每个“Detector”每个样本出现3次,因为它是数据中的复制品。

我需要做的是:

  • 重塑表格,使列包含样本和行Detectors
  • 重命名重复的列,以便我知道它是哪个复制品

我认为DataFrame.pivot可以解决这个问题,但由于数据重复,它失败了。什么是最好的方法?重命名重复,然后重塑,还是有更好的选择?

编辑:我考虑过它,我认为说明目的更好。我需要为每个“样本”存储“探测器”的平均值和标准偏差。

1 个答案:

答案 0 :(得分:6)

您可能正在寻找的是分层索引数据框 [link]

这样的事情会起作用吗?

#build a sample dataframe
a=['P_1']*9
b=[106,106,106,135,135,135,17,17,17]
c = np.random.randint(1,100,9)
df = pandas.DataFrame(data=zip(a,b,c), columns=['sample','detector','cq'])

#add a repetition number column
df['rep_num']=[1,2,3]*( len(df)/3 )

#Convert to a multi-indexed DF
df_multi = df.set_index(['sample','detector','rep_num'])

#--------------Resulting Dataframe---------------------

                             cq
sample detector rep_num    
P_1    106      1        97
                2        83
                3        81
       135      1        46
                2        92
                3        89
       17       1        58
                2        26
                3        75
相关问题