python – Pandas数据透视表ValueError:索引包含重复的条目,无法重新整形

我有一个如下所示的数据帧(前3行):

Sample_Name Sample_ID   Sample_Type IS  Component_Name  IS_Name Component_Group_Name    Outlier_Reasons Actual_Concentration    Area    Height  Retention_Time  Width_at_50_pct Used    Calculated_Concentration    Accuracy
Index                                                               
1   20170824_ELN147926_HexLacCer_Plasma_A-1-1   NaN Unknown True    GluCer(d18:1/12:0)_LCB_264.3    NaN NaN NaN 0.1 2.733532e+06    5.963840e+05    2.963911    0.068676    True    NaN NaN
2   20170824_ELN147926_HexLacCer_Plasma_A-1-1   NaN Unknown True    GluCer(d18:1/17:0)_LCB_264.3    NaN NaN NaN 0.1 2.945190e+06    5.597470e+05    2.745026    0.068086    True    NaN NaN
3   20170824_ELN147926_HexLacCer_Plasma_A-1-1   NaN Unknown False   GluCer(d18:1/16:0)_LCB_264.3    GluCer(d18:1/17:0)_LCB_264.3    NaN NaN NaN 3.993535e+06    8.912731e+05    2.791991    0.059864    True    125.927659773487    NaN

尝试生成数据透视表时:

pivoted_report_conc = raw_report.pivot(index = "Sample_Name", columns = 'Component_Name', values = "Calculated_Concentration")

我收到以下错误:

ValueError: Index contains duplicate entries, cannot reshape

我尝试重置索引但它没有帮助.我在“索引”列中找不到任何重复值.有人可以帮忙找出问题吗?

预期的输出将是一个重新整形的数据框,只有唯一的组件名称作为列和每个样本名称的相应浓度:

Sample_Name    GluCer(d18:1/12:0)_LCB_264.3    GluCer(d18:1/17:0)_LCB_264.3    GluCer(d18:1/16:0)_LCB_264.3
20170824_ELN147926_HexLacCer_Plasma_A-1-1    NaN    NaN    125.927659773487

为了澄清,我不打算聚合数据,只是重塑它.

最佳答案 您可以使用groupby()和unstack()来绕过您在pivot()中看到的错误.

这是一些示例数据,添加了一些边缘情况,并删除了一些列值或替换为MCVE

# df
      Sample_Name  Sample_ID     IS Component_Name Calculated_Concentration Outlier_Reasons
Index                                                                    
1             foo        NaN   True              x                  NaN              NaN  
1             foo        NaN   True              y                  NaN              NaN 
2             foo        NaN   False             z            125.92766              NaN 
2             bar        NaN   False             x                 1.00              NaN  
2             bar        NaN   False             y                 2.00              NaN  
2             bar        NaN   False             z                  NaN              NaN  

(df.groupby(['Sample_Name','Component_Name'])
   .Calculated_Concentration
   .first()
   .unstack()
)

输出:

Component_Name    x   y          z
Sample_Name                       
bar             1.0 2.0        NaN
foo             NaN NaN  125.92766
点赞