How to spread a column in a Pandas data frame

后端 未结 2 853
野性不改
野性不改 2020-11-27 08:22

I have the following pandas data frame:

import pandas as pd
import numpy as np
df = pd.DataFrame({
               \'fc\': [100,100,112,1.3,14,125],
                  


        
2条回答
  •  -上瘾入骨i
    2020-11-27 08:55

    you can also use pd.crosstab() method:

    In [82]: pd.crosstab(index=df.gene_symbol, columns=df.sample_id, 
                         values=df.fc, aggfunc='mean') \
        ...:   .rename_axis(None,1) \
        ...:   .reset_index()
        ...:
    Out[82]:
      gene_symbol     S1     S2
    0           a  100.0    1.3
    1           b  100.0   14.0
    2           c  112.0  125.0
    

提交回复
热议问题