<legend id='2AGds'><style id='2AGds'><dir id='2AGds'><q id='2AGds'></q></dir></style></legend>

      <small id='2AGds'></small><noframes id='2AGds'>

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

        在 groupby 聚合之后指定列顺序

        Specifying column order following groupby aggregation(在 groupby 聚合之后指定列顺序)
            <tbody id='zt9GJ'></tbody>
          • <bdo id='zt9GJ'></bdo><ul id='zt9GJ'></ul>
            • <tfoot id='zt9GJ'></tfoot>
              <i id='zt9GJ'><tr id='zt9GJ'><dt id='zt9GJ'><q id='zt9GJ'><span id='zt9GJ'><b id='zt9GJ'><form id='zt9GJ'><ins id='zt9GJ'></ins><ul id='zt9GJ'></ul><sub id='zt9GJ'></sub></form><legend id='zt9GJ'></legend><bdo id='zt9GJ'><pre id='zt9GJ'><center id='zt9GJ'></center></pre></bdo></b><th id='zt9GJ'></th></span></q></dt></tr></i><div id='zt9GJ'><tfoot id='zt9GJ'></tfoot><dl id='zt9GJ'><fieldset id='zt9GJ'></fieldset></dl></div>

                  <legend id='zt9GJ'><style id='zt9GJ'><dir id='zt9GJ'><q id='zt9GJ'></q></dir></style></legend>

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

                  本文介绍了在 groupby 聚合之后指定列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  每次运行代码时,我的年龄、身高和体重列的顺序都会发生变化.我需要保持 agg 列的顺序不变,因为我最终会根据列位置引用此输出文件.如何确保每次都以相同的顺序输出年龄、身高和体重?

                  The ordering of my age, height and weight columns is changing with each run of the code. I need to keep the order of my agg columns static because I ultimately refer to this output file according to the column locations. What can I do to make sure age, height and weight are output in the same order every time?

                  d = pd.read_csv(input_file, na_values=[''])
                  df = pd.DataFrame(d)
                  df.index_col = ['name', 'address']
                  
                  df_out = df.groupby(df.index_col).agg({'age':np.mean, 'height':np.sum, 'weight':np.sum})
                  df_out.to_csv(output_file, sep=',')
                  

                  推荐答案

                  我觉得你可以使用subset:

                  I think you can use subset:

                  df_out = df.groupby(df.index_col)
                             .agg({'age':np.mean, 'height':np.sum, 'weight':np.sum})[['age','height','weight']]
                  

                  你也可以使用 pandas 函数:

                  Also you can use pandas functions:

                  df_out = df.groupby(df.index_col)
                             .agg({'age':'mean', 'height':sum, 'weight':sum})[['age','height','weight']]
                  

                  示例:

                  df = pd.DataFrame({'name':['q','q','a','a'],
                                     'address':['a','a','s','s'],
                                     'age':[7,8,9,10],
                                     'height':[1,3,5,7],
                                     'weight':[5,3,6,8]})
                  
                  print (df)
                    address  age  height name  weight
                  0       a    7       1    q       5
                  1       a    8       3    q       3
                  2       s    9       5    a       6
                  3       s   10       7    a       8
                  df.index_col = ['name', 'address']
                  df_out = df.groupby(df.index_col)
                             .agg({'age':'mean', 'height':sum, 'weight':sum})[['age','height','weight']]
                  
                  print (df_out)
                                age  height  weight
                  name address                     
                  a    s        9.5      12      14
                  q    a        7.5       4       8
                  

                  根据建议编辑 - 添加 reset_index,如果也需要索引值,这里 as_index=False 不起作用:

                  EDIT by suggestion - add reset_index, here as_index=False does not work if need index values too:

                  df_out = df.groupby(df.index_col)
                             .agg({'age':'mean', 'height':sum, 'weight':sum})[['age','height','weight']]
                             .reset_index()
                  
                  print (df_out)
                    name address  age  height  weight
                  0    a       s  9.5      12      14
                  1    q       a  7.5       4       8
                  

                  这篇关于在 groupby 聚合之后指定列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <tfoot id='8Ykxt'></tfoot>

                    <tbody id='8Ykxt'></tbody>

                    • <bdo id='8Ykxt'></bdo><ul id='8Ykxt'></ul>

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

                        1. <small id='8Ykxt'></small><noframes id='8Ykxt'>

                          <legend id='8Ykxt'><style id='8Ykxt'><dir id='8Ykxt'><q id='8Ykxt'></q></dir></style></legend>