【问题标题】:if else conditions in pandas dataframe and extract column value如果是熊猫数据框中的条件并提取列值
【发布时间】:2020-02-04 01:42:01
【问题描述】:

我有这个数据框(df),看起来像

+-----------------+-----------+----------------+---------------------+--------------+-------------+
|      Gene       | Gene name |     Tissue     |      Cell type      |    Level     | Reliability |
+-----------------+-----------+----------------+---------------------+--------------+-------------+
| ENSG00000001561 | ENPP4     | adipose tissue | adipocytes          | Low          | Approved    |
| ENSG00000001561 | ENPP4     | adrenal gland  | glandular cells     | High         | Approved    |
| ENSG00000001561 | ENPP4     | appendix       | glandular cells     | Medium       | Approved    |
| ENSG00000001561 | ENPP4     | appendix       | lymphoid tissue     | Low          | Approved    |
| ENSG00000001561 | ENPP4     | bone marrow    | hematopoietic cells | Medium       | Approved    |
| ENSG00000002586 | CD99      | adipose tissue | adipocytes          | Low          | Supported   |
| ENSG00000002586 | CD99      | adrenal gland  | glandular cells     | Medium       | Supported   |
| ENSG00000002586 | CD99      | appendix       | glandular cells     | Not detected | Supported   |
| ENSG00000002586 | CD99      | appendix       | lymphoid tissue     | Not detected | Supported   |
| ENSG00000002586 | CD99      | bone marrow    | hematopoietic cells | High         | Supported   |
| ENSG00000002586 | CD99      | breast         | adipocytes          | Not detected | Supported   |
| ENSG00000003056 | M6PR      | adipose tissue | adipocytes          | High         | Approved    |
| ENSG00000003056 | M6PR      | adrenal gland  | glandular cells     | High         | Approved    |
| ENSG00000003056 | M6PR      | appendix       | glandular cells     | High         | Approved    |
| ENSG00000003056 | M6PR      | appendix       | lymphoid tissue     | High         | Approved    |
| ENSG00000003056 | M6PR      | bone marrow    | hematopoietic cells | High         | Approved    |
+-----------------+-----------+----------------+---------------------+--------------+-------------+

预期输出:


+-----------+--------+-------------------------------+
| Gene name | Level  |            Tissue             |
+-----------+--------+-------------------------------+
| ENPP4     | Low    | adipose tissue, appendix      |
| ENPP4     | High   | adrenal gland, bronchus       |
| ENPP4     | Medium | appendix, breast, bone marrow |
| CD99      | Low    | adipose tissue, appendix      |
| CD99      | High   | bone marrow                   |
| CD99      | Medium | adrenal gland                 |
| ...       | ...    | ...                           |
+-----------+--------+-------------------------------+

使用的代码(从 multiple if else conditions in pandas dataframe and derive multiple columns 获得帮助):

def text_df(df):
    if (df[df['Level'].str.match('High')]):
        return (df.assign(Level='High') + df['Tissue'].astype(str))
    elif (df[df['Level'].str.match('Medium')]):
        return (df.assign(Level='Medium') + df['Tissue'].astype(str))
    elif (df[df['Level'].str.match('Low')]):
        return (df.assign(Level='Low') + df['Tissue'].astype(str))

df = df.apply(text_df, axis = 1)

错误:KeyError: ('Level', 'occurred at index 172') 我不明白我做错了什么。有什么建议吗?

【问题讨论】:

    标签: python pandas dataframe if-statement


    【解决方案1】:

    试试:

    df.groupby(['Gene name','Level'], as_index=False)['Cell type'].agg(', '.join)
    

    输出:

    |    | Gene name   | Level        | Cell type                                                                                                       |
    |---:|:------------|:-------------|:----------------------------------------------------------------------------------------------------------------|
    |  0 | CD99        | High         | hematopoietic cells                                                                                             |
    |  1 | CD99        | Low          | adipocytes                                                                                                      |
    |  2 | CD99        | Medium       | glandular cells                                                                                                 |
    |  3 | CD99        | Not detected | glandular cells     ,  lymphoid tissue     ,  adipocytes                                                        |
    |  4 | ENPP4       | High         | glandular cells                                                                                                 |
    |  5 | ENPP4       | Low          | adipocytes          ,  lymphoid tissue                                                                          |
    |  6 | ENPP4       | Medium       | glandular cells     ,  hematopoietic cells                                                                      |
    |  7 | M6PR        | High         | adipocytes          ,  glandular cells     ,  glandular cells     ,  lymphoid tissue     ,  hematopoietic cells |
    

    以下为每个 cmets 添加的更新:

    (df.groupby(['Gene name','Level'], as_index=False)['Cell type']
       .agg(','.join).set_index(['Gene name','Level'])['Cell type']
       .unstack().reset_index())
    

    输出:

    | Gene name   |  High                                                                                                           |  Low                                   |  Medium                                    |  Not detected                                            |
    |:------------|:----------------------------------------------------------------------------------------------------------------|:---------------------------------------|:-------------------------------------------|:---------------------------------------------------------|
    | CD99        | hematopoietic cells                                                                                             | adipocytes                             | glandular cells                            | glandular cells     ,  lymphoid tissue     ,  adipocytes |
    | ENPP4       | glandular cells                                                                                                 | adipocytes          ,  lymphoid tissue | glandular cells     ,  hematopoietic cells | nan                                                      |
    | M6PR        | adipocytes          ,  glandular cells     ,  glandular cells     ,  lymphoid tissue     ,  hematopoietic cells | nan                                    | nan                                        | nan                                                      |
    

    【讨论】:

    • 如果我可以问波士顿,您如何读取数据?我试过 read_clipboard() 但结果不对
    • @sammywemmy 我使用了这些语句...df = pd.read_clipboard(sep='|', header=None) df = df.drop([0,7], axis=1).set_axis(['Gene','Gene name','Tissue','Cell type', 'Level','Reliability'], axis=1) 哦..我没有复制标题内容只是那些数据。
    • @sammywemmy 我也可以做一个 .str.strip 来清理一些空白,但我认为需要做的大部分工作都在上面。
    • @ScottBoston 成功了,谢谢!我正在尝试为其添加另一个复杂性。我希望在新的 df:Gene name | Level:High|Level:Medium|Level:Low 中获得这样的列,并且这些列具有来自第一个 df 的相应值。有什么建议吗?
    • @ScottBoston ..这是一个很棒的班轮。感谢您的帮助(y)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2017-06-26
    • 2016-08-09
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多