【问题标题】:Python melt dataframe based on values of comma-separated character vector columnPython基于逗号分隔的字符向量列的值融化数据框
【发布时间】:2020-05-24 01:46:10
【问题描述】:

我目前正在进行一项测试,其中我有不同的区域以及一些相关的统计数据,以及位于这些区域中的以逗号分隔的基因列表。此列表的数量是可变的,并且可能不包含任何内容 ("NA")。

我怎样才能“融化”这个数据框:

 region_id  statistic      genelist
          1        2.5       A, B, C
          2        0.5    B, C, D, E
          3        3.2          <NA>
          4        0.1          E, F

变成这样:

     region_id  statistic gene
           1       2.5    A
           1       2.5    B
           1       2.5    C
           2       0.5    B
           2       0.5    C
           2       0.5    D
           2       0.5    E
           3       3.2 <NA>
           4       0.1    E
           4       0.1    F

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用下面的代码,使用stack堆叠它,然后在', '上拆分,然后再次堆叠它,因为我们堆叠了两次,使用unstack-2取消堆叠,然后重置索引使用reset_index-1,然后在没有参数的情况下执行最终的reset_index

    print(df.set_index(['region_id', 'statistic'])
       .stack()
       .str.split(', ', expand=True)
       .stack()
       .unstack(-2)
       .reset_index(-1, drop=True)
       .reset_index()
    )
    

    【讨论】:

    • 如果您提供代码方法的解释,那就太好了。
    • @shaikmoed 编辑我的
    【解决方案2】:

    用途:

    # Splitting on , and joining with region_id and statistic columns
    val = pd.concat([df.region_id, 
                     df.statistic, 
                     df.genelist.str.split(',', expand=True)], 
                    axis=1)
    
    # Unpivoting and ignoring variable column
    m = pd.melt(val, id_vars=['region_id', 'statistic'])\
                .loc[:, ['region_id', 'statistic', 'value']]
    
    # Ignoring Null values and sorting based on region_id
    m[m.value.notnull()]\
    .sort_values('region_id')\
    .reset_index(drop=True)\
    .rename(columns={'value':'gene'})
    
     region_id  statistic gene
           1       2.5    A
           1       2.5    B
           1       2.5    C
           2       0.5    B
           2       0.5    C
           2       0.5    D
           2       0.5    E
           3       3.2 <NA>
           4       0.1    E
           4       0.1    F
    

    【讨论】:

      【解决方案3】:

      使用stack

      df=df.join(df.pop('genelist').str.split(',',expand=True))
      df.set_index(['region_id','statistic']).stack().reset_index(level=[0,1],name='gene')
      

      使用melt

      df=df.join(df.pop('genelist').str.split(',',expand=True))
      pd.melt(df,id_vars=['region_id','statistic'],value_name='gene').dropna()
      

      【讨论】:

        【解决方案4】:

        您也可以使用df.assignexplode 来执行此操作。鉴于数据采用列表格式,Explode 用于将列数据分成多行。 基因列表中的每个数据都可以转换为列表,使用逗号分隔,然后在基因列表列上使用explode。

        (df.assign(genelist=df.genelist.str.split(',')).explode('genelist'))
        

        【讨论】:

          猜你喜欢
          • 2012-09-19
          • 2013-07-14
          • 2022-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-17
          • 2019-08-07
          • 2021-11-02
          相关资源
          最近更新 更多