我有以下数据框(pandas版本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行x 8列]
到上面的数据框
我需要最终将其改为以下格式
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('组&#39)
代表g,v分组: pandas.pivot_table(数据= V,值= [' p值'' pValueString']),行数= ['组''开始&#39 ;,'结束&#39],COLS = ['样品'])[' pValueString']
如何获得相应的desc和状态?
答案 0 :(得分:0)
对于pandas数据透视表,您可以将所需的行传递为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
然后为你的