• <tfoot id='Am4bE'></tfoot>

    <small id='Am4bE'></small><noframes id='Am4bE'>

  • <i id='Am4bE'><tr id='Am4bE'><dt id='Am4bE'><q id='Am4bE'><span id='Am4bE'><b id='Am4bE'><form id='Am4bE'><ins id='Am4bE'></ins><ul id='Am4bE'></ul><sub id='Am4bE'></sub></form><legend id='Am4bE'></legend><bdo id='Am4bE'><pre id='Am4bE'><center id='Am4bE'></center></pre></bdo></b><th id='Am4bE'></th></span></q></dt></tr></i><div id='Am4bE'><tfoot id='Am4bE'></tfoot><dl id='Am4bE'><fieldset id='Am4bE'></fieldset></dl></div>
      <legend id='Am4bE'><style id='Am4bE'><dir id='Am4bE'><q id='Am4bE'></q></dir></style></legend>
      • <bdo id='Am4bE'></bdo><ul id='Am4bE'></ul>

        Pandas - 按列分组并将数据转换为 numpy 数组

        时间:2023-10-19
        <tfoot id='Drd4T'></tfoot>
        <i id='Drd4T'><tr id='Drd4T'><dt id='Drd4T'><q id='Drd4T'><span id='Drd4T'><b id='Drd4T'><form id='Drd4T'><ins id='Drd4T'></ins><ul id='Drd4T'></ul><sub id='Drd4T'></sub></form><legend id='Drd4T'></legend><bdo id='Drd4T'><pre id='Drd4T'><center id='Drd4T'></center></pre></bdo></b><th id='Drd4T'></th></span></q></dt></tr></i><div id='Drd4T'><tfoot id='Drd4T'></tfoot><dl id='Drd4T'><fieldset id='Drd4T'></fieldset></dl></div>
      1. <legend id='Drd4T'><style id='Drd4T'><dir id='Drd4T'><q id='Drd4T'></q></dir></style></legend>
          <bdo id='Drd4T'></bdo><ul id='Drd4T'></ul>
            <tbody id='Drd4T'></tbody>

            <small id='Drd4T'></small><noframes id='Drd4T'>

                  本文介绍了Pandas - 按列分组并将数据转换为 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Having the following data frame, group A have 4 samples, B 3 samples and C 1 sample:

                    group   data_1   data_2
                  0     A        1        4
                  1     A        2        5
                  2     A        3        6
                  3     A        4        7
                  4     B        1        4
                  5     B        2        5
                  6     B        3        6
                  7     C        1        4
                  

                  I would like to transform the data into numpy array, where each row is a group with all its samples and zero padding for groups that have fewer samples.

                  Resulting in an array like so:

                  [
                     [[1,4],[2,5],[3,6],[4,7]], # this is A group 4 samples
                     [[1,4],[2,5],[3,6],[0,0]], # this is B group 3 samples
                     [[1,4],[0,0],[0,0],[0,0]], # this is C group 1 sample
                  ]
                  

                  解决方案

                  First is necessary add missing values - first solution with unstack and stack, counter Series is created by cumcount.

                  Second solution use reindex by MultiIndex.

                  Last use lambda function with groupby, convert to numpy array by values and last to lists:

                  g = df.groupby('group').cumcount()
                  L = (df.set_index(['group',g])
                         .unstack(fill_value=0)
                         .stack().groupby(level=0)
                         .apply(lambda x: x.values.tolist())
                         .tolist())
                  print (L)
                  
                  [[[1, 4], [2, 5], [3, 6], [4, 7]], 
                   [[1, 4], [2, 5], [3, 6], [0, 0]], 
                   [[1, 4], [0, 0], [0, 0], [0, 0]]]
                  

                  Another solution:

                  g = df.groupby('group').cumcount()
                  mux = pd.MultiIndex.from_product([df['group'].unique(), g.unique()])
                  L = (df.set_index(['group',g])
                         .reindex(mux, fill_value=0)
                         .groupby(level=0)['data_1','data_2']
                         .apply(lambda x: x.values.tolist())
                         .tolist()
                  )
                  

                  这篇关于Pandas - 按列分组并将数据转换为 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Pandas 中,我如何将选择日期列转换为行 下一篇:在 Python 中使用命令模式执行/撤消

                  相关文章

                      • <bdo id='CvLT6'></bdo><ul id='CvLT6'></ul>

                      <i id='CvLT6'><tr id='CvLT6'><dt id='CvLT6'><q id='CvLT6'><span id='CvLT6'><b id='CvLT6'><form id='CvLT6'><ins id='CvLT6'></ins><ul id='CvLT6'></ul><sub id='CvLT6'></sub></form><legend id='CvLT6'></legend><bdo id='CvLT6'><pre id='CvLT6'><center id='CvLT6'></center></pre></bdo></b><th id='CvLT6'></th></span></q></dt></tr></i><div id='CvLT6'><tfoot id='CvLT6'></tfoot><dl id='CvLT6'><fieldset id='CvLT6'></fieldset></dl></div>

                      <small id='CvLT6'></small><noframes id='CvLT6'>

                    1. <legend id='CvLT6'><style id='CvLT6'><dir id='CvLT6'><q id='CvLT6'></q></dir></style></legend><tfoot id='CvLT6'></tfoot>