0
我有一个看起来像这两CSV数据的分组平均数据框架柱:如何基于另一个数据框
gene,stem1,stem2,stem3,b1,b2,b3,t1
foo,20,10,11,23,22,79,3
bar,17,13,505,12,13,88,1
qui,17,13,5,12,13,88,3
而且这样的:
celltype,phenotype
SC,stem1
BC,b2
SC,stem2
SC,stem3
BC,b1
TC,t1
BC,b3
数据帧这个样子的:
In [5]: import pandas as pd
In [7]: main_df = pd.read_table("http://dpaste.com/2MRRRM3.txt", sep=",")
In [8]: main_df
Out[8]:
gene stem1 stem2 stem3 b1 b2 b3 t1
0 foo 20 10 11 23 22 79 3
1 bar 17 13 505 12 13 88 1
2 qui 17 13 5 12 13 88 3
In [11]: source_df = pd.read_table("http://dpaste.com/091PNE5.txt", sep=",")
In [12]: source_df
Out[12]:
celltype phenotype
0 SC stem1
1 BC b2
2 SC stem2
3 SC stem3
4 BC b1
5 TC t1
6 BC b3
我想要做的是基于分组在main_df
以平均每列在source_df
。所以最终看起来像这样:
SC BC TC
foo (20+10+11)/3 (23+22+79)/3 3/1
bar (17+13+505)/3 (12+13+88)/3 1/1
qui (17+13+5)/3 (12+13+88)/3 3/1
我该如何做到这一点?