【发布时间】:2017-05-01 20:50:25
【问题描述】:
非索引 df 包含基因行、包含该基因突变的单元格以及该基因中的突变类型:
df = pd.DataFrame({'gene': ['one','one','one','two','two','two','three'],
'cell': ['A', 'A', 'C', 'A', 'B', 'C','A'],
'mutation': ['frameshift', 'missense', 'nonsense', '3UTR', '3UTR', '3UTR', '3UTR']})
df:
cell gene mutation
0 A one frameshift
1 A one missense
2 C one nonsense
3 A two 3UTR
4 B two 3UTR
5 C two 3UTR
6 A three 3UTR
我想旋转这个 df,以便我可以按基因索引并将列设置为单元格。问题是每个单元格可以有多个条目:给定单元格中的任何一个基因都可以有多个突变(单元格 A 在基因 One 中有两个不同的突变)。所以当我跑步时:
df.pivot_table(index='gene', columns='cell', values='mutation')
发生这种情况:
DataError: No numeric types to aggregate
我想使用掩码来执行枢轴,同时捕获至少一个突变的存在:
A B C
gene
one 1 1 1
two 0 1 0
three 1 1 0
【问题讨论】:
标签: python pandas group-by pivot-table reshape