【问题标题】:converting wide matrix in to condense sparse format将宽矩阵转换为压缩稀疏格式
【发布时间】:2020-07-11 21:57:35
【问题描述】:

我有一个如下所示的宽稀疏数据框

pd.DataFrame({"B.count": [0, 0, 1, 0, 0],
              "B.score": [0, 0, 87,0 ,0],
              "C.count": [0, 1, 0, 1, 0],
              "C.score": [0, 91, 0, 14, 0],
              "D.count": [1, 0, 10, 0, 11],
              "D.score": [93, 0, 3, 0, 4]}, 
               index = [1,2,3,4,5])

我想把它转换成一个长密的稀疏格式。

pd.DataFrame({"id": [1, 2, 3, 3, 4, 5],
              "taste": ["D", "C", "B", "D", "C", "D"],
              "count": [1, 1, 1, 10, 1, 11],
              "score": [93, 91, 87, 3, 14, 4]})

看来解决方案必须通过wide_to_long函数,但不幸的是,我无法使其工作。

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    让我们构建一个自定义的wide_to_long

    # mask the 0 with nan, stack to get rid of the nan's
    s = df.where(df>0).stack().reset_index()
    
    # output dataframe
    (pd.concat((s.rename(columns={'level_0':'id'}),
                s.level_1.str.extract('(?P<taste>.+)\.(?P<type>count|score)$')
               ), axis=1
              )
      .pivot_table(index=['id','taste'], columns='type',values=0 )
      .reset_index()
    )
    

    输出:

    type  id taste  count  score
    0      1     D    1.0   93.0
    1      2     C    1.0   91.0
    2      3     B    1.0   87.0
    3      3     D   10.0    3.0
    4      4     C    1.0   14.0
    5      5     D   11.0    4.0
    

    【讨论】:

      【解决方案2】:

      是的,你可以使用 pandas wide to long;但是,您必须对列的顺序稍作调整:

      #made a change to the positioning of values in the columns
      #get the words after the dot to come before words before the dot
      #makes it easier to use pandas wide to long
      df.columns = [F'{i[2:]}.{i[0]}' for i in df.columns]
      
      #create id column
      df = df.assign(id=df.index)
      
      #convert from wide to long
      (pd.wide_to_long(df,
                       stubnames=['count','score'],
                       sep='.',
                       i='id',
                       j='taste', 
                       suffix='[A-Z]')
       #remove 0 values
       .query('count != 0')
       .sort_index()
       .reset_index()
      )
      
          id  taste   count   score
      0   1   D       1       93
      1   2   C       1       91
      2   3   B       1       87
      3   3   D       10      3
      4   4   C       1       14
      5   5   D       11      4
      

      【讨论】:

        猜你喜欢
        • 2011-10-02
        • 2015-12-05
        • 1970-01-01
        • 2023-04-10
        • 2021-11-25
        • 2017-07-02
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        相关资源
        最近更新 更多