我想使用 groupby 计算数字列的平均值,但保留所有列。来自 7 列的数据框示例如下:
tracking_id gene_id gene_short_name tss_id locus FPKM-1 FPKM-2 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Tss1231 1:4490927-4496413 0.611985 232 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Ts412 1:4490927-4496413 12 21 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Ts56 1:4490927-4496413 2 213 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 TS512 1:4490927-4496413 0.611985 5 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 TS12241 1:4490927-4496413 0.611985 51 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS124 1:4529016-4529123 35 1 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS-1824 1:4529016-4529123 1 2 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS1249082 1:4529016-4529123 2 5 ENSMUSG00000088000 ENSMUSG00000088000 Gm25493 TS1290328 1:4723276-4723379 0 1 ENSMUSG00000098104 ENSMUSG00000098104 Gm6085 TS01239-1 1:4687933-4689403 0.0743559 6 ENSMUSG00000033845 ENSMUSG00000033845 Mrpl15 TSS31014,TSS82987,TSS82990,TSS86849 1:4773205-4785739 79.1154 7 ENSMUSG00000093015 ENSMUSG00000093015 Gm22463 TSS79849 1:5644644-5644745 0 1 ENSMUSG00000025905 ENSMUSG00000025905 Oprk1 TSS15316,TSS3878,TSS6226,TSS65522 1:5588492-5606131 0 6 ENSMUSG00000033774 ENSMUSG00000033774 Npbwr1 TSS69693 1:5913706-5917398 0 8 ENSMUSG00000033793 ENSMUSG00000033793 Atp6v1h TSS4651 1:5083172-5162549 24.2386 9 ENSMUSG00000087247 ENSMUSG00000087247 Fam150a TSS42747 1:6359330-6394731 0.502804 1
I would like to group by the first 3 columns, and keep columns 4 and 5 in my output (best would be the first row of each repeated columns 1 to 3) and then calculate the mean of the numeric columns at the end. I have written this:
import pandas as pd
df = pd.read_table('grouping.txt')
grouped = df.groupby(list(df.columns[0:3]), sort=False).mean()
输出是:
tracking_id gene_id gene_short_name FPKM-1 FPKM-2 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 3.167191 104.4 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 12.66666667 2.666666667 ENSMUSG00000088000 ENSMUSG00000088000 Gm25493 0 1 ENSMUSG00000098104 ENSMUSG00000098104 Gm6085 0.0743559 6 ENSMUSG00000033845 ENSMUSG00000033845 Mrpl15 79.1154 7 ENSMUSG00000093015 ENSMUSG00000093015 Gm22463 0 1 ENSMUSG00000025905 ENSMUSG00000025905 Oprk1 0 6 ENSMUSG00000033774 ENSMUSG00000033774 Npbwr1 0 8 ENSMUSG00000033793 ENSMUSG00000033793 Atp6v1h 24.2386 9 ENSMUSG00000087247 ENSMUSG00000087247 Fam150a 0.502804 1
以上是输出,但缺少输入文件的第 4 列(TSS)和第 5 列(基因座)。我怎样才能保留这两列(它们的值不同,因此不能成为 groupby 列的一部分。保留任何列的值对我来说都是可以的,只要有一个分组依据)。
请您参考如下方法:
您可以将 groupby() 聚合的结果合并回原始 DataFrame 的去重版本。也许是这样的:
# identify the columns we want to aggregate by; this could
# equivalently be defined as list(df.columns[0:3])
group_cols = ['tracking_id', 'gene_id', 'gene_short_name']
# identify the columns which we want to average; this could
# equivalently be defined as list(df.columns[4:])
metric_cols = ['FPKM-1', 'FPKM-2']
# create a new DataFrame with a MultiIndex consisting of the group_cols
# and a column for the mean of each column in metric_cols
aggs = df.groupby(group_cols)[metric_cols].mean()
# remove the metric_cols from df because we are going to replace them
# with the means in aggs
df.drop(metric_cols, axis=1, inplace=True)
# dedupe to leave only one row with each combination of group_cols
# in df
df.drop_duplicates(subset=group_cols, keep='last', inplace=True)
# add the mean columns from aggs into df
df = df.merge(right=aggs, right_index=True, left_on=group_cols, how='right')
