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

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

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

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

      <tfoot id='NuJES'></tfoot>
      1. 如何循环分组的 Pandas 数据框?

        How to loop over grouped Pandas dataframe?(如何循环分组的 Pandas 数据框?)
      2. <legend id='fO25L'><style id='fO25L'><dir id='fO25L'><q id='fO25L'></q></dir></style></legend>

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

              <tbody id='fO25L'></tbody>

              <bdo id='fO25L'></bdo><ul id='fO25L'></ul>

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

              <tfoot id='fO25L'></tfoot>
                  本文介绍了如何循环分组的 Pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  数据帧:

                    c_os_family_ss c_os_major_is l_customer_id_i
                  0      Windows 7                         90418
                  1      Windows 7                         90418
                  2      Windows 7                         90418
                  

                  代码:

                  print df
                  for name, group in df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)):
                      print name
                      print group
                  

                  我正在尝试遍历聚合数据,但出现错误:

                  I'm trying to just loop over the aggregated data, but I get the error:

                  ValueError:解包的值太多

                  ValueError: too many values to unpack

                  @EdChum,这是预期的输出:

                  @EdChum, here's the expected output:

                                                                      c_os_family_ss  
                  l_customer_id_i
                  131572           Windows 7,Windows 7,Windows 7,Windows 7,Window...
                  135467           Windows 7,Windows 7,Windows 7,Windows 7,Window...
                  
                                                                       c_os_major_is
                  l_customer_id_i
                  131572           ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...
                  135467           ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...
                  

                  输出不是问题,我希望循环遍历每个组.

                  The output is not the problem, I wish to loop over every group.

                  推荐答案

                  df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)) 确实已经返回一个数据框,所以你不能再循环组了.

                  df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)) does already return a dataframe, so you cannot loop over the groups anymore.

                  一般:

                  • df.groupby(...) 返回一个 GroupBy 对象(DataFrameGroupBy 或 SeriesGroupBy),通过这个,您可以遍历组(如文档此处中所述).您可以执行以下操作:

                  • df.groupby(...) returns a GroupBy object (a DataFrameGroupBy or SeriesGroupBy), and with this, you can iterate through the groups (as explained in the docs here). You can do something like:

                  grouped = df.groupby('A')
                  
                  for name, group in grouped:
                      ...
                  

                • 当您在 groupby 上应用函数时,在您的示例中 df.groupby(...).agg(...) (但这也可以是 transform, apply, mean, ...),你组合应用函数的结果将不同的组放在一个数据框中(groupby 的split-apply-combine"范式的应用和组合步骤).因此,其结果将始终是 DataFrame(或 Series,具体取决于应用的功能).

                • When you apply a function on the groupby, in your example df.groupby(...).agg(...) (but this can also be transform, apply, mean, ...), you combine the result of applying the function to the different groups together in one dataframe (the apply and combine step of the 'split-apply-combine' paradigm of groupby). So the result of this will always be again a DataFrame (or a Series depending on the applied function).

                  这篇关于如何循环分组的 Pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  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 库)

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

                    <tbody id='Y3R7J'></tbody>

                        <legend id='Y3R7J'><style id='Y3R7J'><dir id='Y3R7J'><q id='Y3R7J'></q></dir></style></legend>
                        • <bdo id='Y3R7J'></bdo><ul id='Y3R7J'></ul>

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