• <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 数组

        Pandas - group by column and transform the data to numpy array(Pandas - 按列分组并将数据转换为 numpy 数组)
        <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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                      • <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'>

                            <tbody id='CvLT6'></tbody>
                        • <legend id='CvLT6'><style id='CvLT6'><dir id='CvLT6'><q id='CvLT6'></q></dir></style></legend><tfoot id='CvLT6'></tfoot>