按行名修改pandas数据帧

时间:2017-06-16 14:35:24

标签: python pandas

首先,我认为问题标题并没有很好地解释这个问题。请随时更改标题或推荐更好的标题。

我正在阅读格式的CSV文件: enter image description here

"sample","module","status","tot.seq","seq.length","pct.gc","pct.dup"
"ERR435952_cleaned_1","Basic Statistics","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Per base sequence quality","FAIL","15529112","62",47,41.66
"ERR435952_cleaned_1","Per tile sequence quality","FAIL","15529112","62",47,41.66
"ERR435952_cleaned_1","Per sequence quality scores","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Per base sequence content","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Per sequence GC content","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Per base N content","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Sequence Length Distribution","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Sequence Duplication Levels","WARN","15529112","62",47,41.66
"ERR435952_cleaned_1","Overrepresented sequences","WARN","15529112","62",47,41.66
"ERR435952_cleaned_1","Adapter Content","PASS","15529112","62",47,41.66
"ERR435952_cleaned_1","Kmer Content","FAIL","15529112","62",47,41.66
"ERR435952_cleaned_2","Basic Statistics","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Per base sequence quality","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Per tile sequence quality","WARN","15529112","62",48,42.44
"ERR435952_cleaned_2","Per sequence quality scores","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Per base sequence content","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Per sequence GC content","WARN","15529112","62",48,42.44
"ERR435952_cleaned_2","Per base N content","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Sequence Length Distribution","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Sequence Duplication Levels","WARN","15529112","62",48,42.44
"ERR435952_cleaned_2","Overrepresented sequences","WARN","15529112","62",48,42.44
"ERR435952_cleaned_2","Adapter Content","PASS","15529112","62",48,42.44
"ERR435952_cleaned_2","Kmer Content","FAIL","15529112","62",48,42.44

我想把它转换成这样的东西,所以我可以根据PASS / FAIL / WARN值创建一个简单的热图(包括读取总数:tot.seq): enter image description here

我知道我可以通过计算行数(每个模块/特征值的间隔之间存在相关性)来做到这一点,但这不是很整齐,我不确定它是否对大数据集。有没有办法根据名称映射值,而不是遵循间隔(即i,i + n ......等等)

1 个答案:

答案 0 :(得分:2)

使用set_index + unstack,同时为索引中的列添加reset_index,为module添加rename_axis - 列名:

df = df.set_index(['sample', 'tot.seq', 'module'])['status'].unstack() \
       .reset_index().rename_axis(None, axis=1)
print (df)
                sample   tot.seq Adapter Content Basic Statistics  \
0  ERR435952_cleaned_1  15529112            PASS             PASS   
1  ERR435952_cleaned_2  15529112            PASS             PASS   

  Kmer Content Overrepresented sequences Per base N content  \
0         FAIL                      WARN               PASS   
1         FAIL                      WARN               PASS   

  Per base sequence content Per base sequence quality Per sequence GC content  \
0                      PASS                      FAIL                    PASS   
1                      PASS                      PASS                    WARN   

  Per sequence quality scores Per tile sequence quality  \
0                        PASS                      FAIL   
1                        PASS                      WARN   

  Sequence Duplication Levels Sequence Length Distribution  
0                        WARN                         PASS  
1                        WARN                         PASS  

但如果得到:

  

ValueError:索引包含重复的条目,无法重塑

然后有重复项并需要汇总数据:

print (df)
                sample                       module status   tot.seq  \
0  ERR435952_cleaned_1             Basic Statistics   PASS  15529112   
1  ERR435952_cleaned_1    Per base sequence quality   FAIL  15529112   
2  ERR435952_cleaned_1    Per base sequence quality   FAIL  15529112   
3  ERR435952_cleaned_1  Per sequence quality scores   PASS  15529112   

   seq.length  pct.gc  pct.dup  
0          62      47    41.66  
1          62      47    41.66  
2          62      47    41.66  
3          62      47    41.66  

df = df.pivot_table(index=['sample', 'tot.seq'], columns='module', values='status', aggfunc=', '.join) \
       .reset_index().rename_axis(None, axis=1)
print (df)
                sample   tot.seq Basic Statistics Per base sequence quality  \
0  ERR435952_cleaned_1  15529112             PASS                FAIL, FAIL   

  Per sequence quality scores  
0                        PASS  
df = df.groupby(['sample', 'tot.seq', 'module'])['status'].apply(', '.join).unstack() \
       .reset_index().rename_axis(None, axis=1)
print (df)

                sample   tot.seq Basic Statistics Per base sequence quality  \
0  ERR435952_cleaned_1  15529112             PASS                FAIL, FAIL   

  Per sequence quality scores  
0                        PASS