2015-06-09 108 views
0

我有以下數據幀(熊貓版本0.13.1)重塑和重新佈置熊貓

>>> import pandas as pd 
>>> DF = pd.DataFrame({'Group':['G1','G1','G2','G2'],'Start':['10','10','12','13'],'End':['13','13','14','15'],'Sample':['S1','S2','S3','S3'],'Status':['yes','yes','no','yes'],'pValue':[0.13,0.12,0.96,0.76],'pValueString':['13/100','12/100','96/100','76/100'],'desc':['aaaaaa','bbbbbb','aaaaaa','cccccc']}) 
>>> DF 
    End Group Sample Start Status pValue pValueString desc 
0 13 G1  S1 10 yes 0.13  13/100 aaaaaa 
1 13 G1  S2 10  no 0.12  12/100 bbbbbb 
2 14 G2  S3 12  no 0.96  96/100 aaaaaa 
3 15 G2  S3 13 yes 0.76  76/100 cccccc 

[4行×8列]

向數據幀以上

  1. 我想要groupby'Group'。
  2. 然後groupby一個起始對聯。
  3. 旋轉每個組的樣本值。由max(pValue)彙總
  4. 獲取對應於具有較高pvalue的採樣的相應Status和desc,並用pValueString替換其值。

我需要最終得到這個以下格式

Group Start End Sample   Status desc 
        S1 S2 
G1 10 13 13/100 12/100 yes  aaaaaa 
        S3 
G2 12 14 96/100   no  aaaaaa 
     13 15 76/100   yes  cccccc 

我曾嘗試使用pivot_table和GROUPBY,但無濟於事。 任何幫助將不勝感激。

我有

分組= DF.groupby( '組')

爲G,V在分組: pandas.pivot_table(數據= V,值= [ 'p值' ,'pValueString']),rows = ['Group','Start','End'],cols = ['Sample'])['pValueString']

如何獲得相應的desc和Status?

回答

0

大熊貓透視表,你傳遞你想爲index行和你想要的列作爲colums

pvt = DF.pivot_table(index = ['Group','Start','End','Status'], columns = ['Sample']) 
pvt 
Out[209]: 
         pValue    
Sample      S1 S2 S3 
Group Start End Status     
G1 10 13 yes  0.13 0.12 NaN 
G2 12 14 no  NaN NaN 0.96 
     13 15 yes  NaN NaN 0.76 

然後你

+0

謝謝。我也有這個。但那正是我所尋找的 – user2755526