• <legend id='GJgqr'><style id='GJgqr'><dir id='GJgqr'><q id='GJgqr'></q></dir></style></legend>

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

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

      • <bdo id='GJgqr'></bdo><ul id='GJgqr'></ul>
      <tfoot id='GJgqr'></tfoot>

      1. Python:一次包含重复值的多个列的 Pandas 数据透视表

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

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

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

                <tbody id='mrsWG'></tbody>

                1. 本文介绍了Python:一次包含重复值的多个列的 Pandas 数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有一个包含名称、学校和标记列的 pandas 数据框

                  have a pandas dataframme with columns name , school and marks

                  name  school  marks
                  
                  tom     HBS     55
                  tom     HBS     55
                  tom     HBS     14
                  mark    HBS     28
                  mark    HBS     19
                  lewis   HBS     88
                  

                  如何转置和转换成这样的

                  How to transpose and convert into like this

                  name  school  marks_1 marks_2 marks_3
                  
                  tom     HBS     55     55       14
                  mark    HBS     28     19
                  lewis   HBS     88
                  

                  试过这个:

                  df = df.pivot_table(index='name', values='marks', columns='school') 
                      .reset_index() 
                      .rename_axis(None, axis=1)
                  
                  print(df)
                  

                  df = df.pivot('name','marks','school')
                  

                  检查了这些链接

                  https://stackoverflow.com/questions/22798934/pandas-long-to-wide-reshape-by-two-variables
                  https://stackoverflow.com/questions/62391419/pandas-group-by-and-convert-rows-into-multiple-columns
                  https://stackoverflow.com/questions/60698109/pandas-multiple-rows-to-single-row-with-multiple-columns-on-2-indexes
                  

                  由于重复值而出现此错误.如果存在重复如何处理,我们必须保留它们

                  getting this error due to duplicate values. how to handle if duplicate exists and we have to keep them

                  ValueError: Index contains duplicate entries, cannot reshape
                  

                  推荐答案

                  尝试使用 set_indexunstackgroupbycumcount:

                  Try using set_index and unstack with groupby and cumcount:

                  df_out = df.set_index(['name',
                                         'school',
                                         df.groupby(['name','school'])
                             .cumcount() +1]).unstack()
                  df_out.columns = [f'{i}_{j}' for i, j in df_out.columns]
                  df_out = df_out.reset_index()
                  df_out
                  

                  输出:

                      name school  marks_1  marks_2  marks_3
                  0  lewis    HBS     88.0      NaN      NaN
                  1   mark    HBS     28.0     19.0      NaN
                  2    tom    HBS     55.0     55.0     14.0
                  

                  这篇关于Python:一次包含重复值的多个列的 Pandas 数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:get_dummies 和一起计数 下一篇:如何在 pandas 数据框中对最大和最小时间戳进行分组

                  相关文章

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

                    1. <small id='hTUPZ'></small><noframes id='hTUPZ'>

                      <tfoot id='hTUPZ'></tfoot>
                    2. <legend id='hTUPZ'><style id='hTUPZ'><dir id='hTUPZ'><q id='hTUPZ'></q></dir></style></legend>

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