【问题标题】:convert R aggregative function to python将 R 聚合函数转换为 python
【发布时间】:2022-01-03 18:52:09
【问题描述】:

我现在在 R 中预处理我的数据,然后将其导入 python,但是如果我可以在 python 中完成所有操作会更方便。有人知道如何将以下代码转换为 python 吗?

aggregate(mrna[,c(2:178)],by=list(mrna$GeneSymbol), FUN=function(x) x=max(x))

谢谢!

编辑: mrna的例子

GeneSymbol TCGA.2J.AAB1.01A TCGA.2J.AAB4.01A TCGA.2J.AAB6.01A TCGA.2J.AAB8.01A TCGA.2J.AAB9.01A
   A1BG          81.9122          56.7551          82.5497          56.9307         105.7878
    A1CF          25.3659          53.4512           8.1871          33.8425          21.4362
 GGACT         180.4976         111.0774         163.1228         185.8143         166.7095
    A2M       19703.8049       15837.8241        8517.4444       14413.9130       24311.7792
 A2ML1          85.8537           0.0000        1815.7895          16.9213         642.0150

【问题讨论】:

  • 您能展示一下您的数据框或对象mrna 的样子吗?
  • 这是足够的信息还是您想要更多?
  • 您能否提供您希望最终数据框的外观?你想在每一列中找到最大值吗?

标签: python r pandas aggregate


【解决方案1】:

在 R 中:

mrna = data.frame(GeneSymbol = c("A","B","C","B","A"),
                  S1 = 1:5,
                  S2 = 6:10,
                  S3 = 11:15)

aggregate(mrna[,c(2:4)],by=list(mrna$GeneSymbol), FUN=function(x) x=max(x))

Group.1 S1  S2  S3
<chr>   <int>   <int>   <int>
A   5   10  15
B   4   9   14
C   3   8   13

在python中

import pandas as pd
import numpy as np
mrna = pd.DataFrame({'GeneSymbol':["A","B","C","B","A"]})
mrna[['S1','S2','S3']] = np.arange(1,16).reshape(3,5).T

mrna.groupby('GeneSymbol').agg('max').reset_index()
  GeneSymbol  S1  S2  S3
0          A   5  10  15
1          B   4   9  14
2          C   3   8  13

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2016-02-26
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多