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

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

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

      <tfoot id='TuC6O'></tfoot>
          <bdo id='TuC6O'></bdo><ul id='TuC6O'></ul>

        按时间值进行透视排序 - Pandas

        时间:2023-10-19
        <legend id='EwvzK'><style id='EwvzK'><dir id='EwvzK'><q id='EwvzK'></q></dir></style></legend>
          <tbody id='EwvzK'></tbody>

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

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

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

                1. 本文介绍了按时间值进行透视排序 - Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想 pivot 一个 df 并显示基于时间值的值,而不是列值.

                  I want to pivot a df and display values based off time values, not column values.

                  df = pd.DataFrame({
                      'Place' : ['John','Alan','Cory','Jim','John','Alan','Cory','Jim'],                                
                      'Number' : ['2','3','5','5','3','4','6','6'],          
                      'Code' : ['1','2','3','4','1','2','3','4'],                      
                      'Time' : ['1904-01-01 08:00:00','1904-01-01 09:00:00','1904-01-02 01:00:00','1904-01-02 02:00:00','1904-01-01 08:10:00','1904-01-01 09:10:00','1904-01-02 01:10:00','1904-01-02 02:10:00'],                           
                      })
                  
                  df = df.pivot_table(index = 'Number', columns = 'Place', values = 'Time', aggfunc = 'first').fillna('')
                  

                  输出:

                  Place                  Alan                 Cory                  Jim                 John
                  Number                                                                                    
                  2                                                                      1904-01-01 08:00:00
                  3       1904-01-01 09:00:00                                            1904-01-01 08:10:00
                  4       1904-01-01 09:10:00                                                               
                  5                            1904-01-02 01:00:00  1904-01-02 02:00:00                     
                  6                            1904-01-02 01:10:00  1904-01-02 02:10:00 
                  

                  预期输出:

                  Place                  John                 Alan                 Cory                  Jim
                  Number                                                                                    
                  2       1904-01-01 08:00:00                                                               
                  3       1904-01-01 08:10:00  1904-01-01 09:00:00                                          
                  4                            1904-01-01 09:10:00                                          
                  5                                                 1904-01-02 01:00:00  1904-01-02 02:00:00
                  6                                                 1904-01-02 01:10:00  1904-01-02 02:10:00             
                  

                  注意:我只添加了一个虚拟日期来区分午夜之后的时间.一旦 df 被适当地排序,我最终会放弃日期并留下时间.

                  Note: I've only added a dummy dates to differentiate for times after midnight. I will eventually drop the dates and just leave the times once the df is appropriately sorted.

                  推荐答案

                  不幸的是 pivot_table 默认对列名进行排序,并且没有参数以避免它.所以可能的解决方案是 DataFrame.reindexPlace 的原始唯一值:

                  Unfortunately pivot_table sorting columns names by default and no paramater for avoid it. So possible solution is DataFrame.reindex by original unique values of column Place:

                  #if necessary convert to datetimes and sorting
                  df['Time'] = pd.to_datetime(df['Time'])
                  df = df.sort_values('Time')
                  df1 = df.pivot_table(index='Number',columns='Place',values='Time',aggfunc='first').fillna('')
                  
                  df1 = df1.reindex(columns=df['Place'].unique())
                  print (df1)
                  Place                  John                 Alan                 Cory  
                  Number                                                                  
                  2       1904-01-01 08:00:00                                             
                  3       1904-01-01 08:10:00  1904-01-01 09:00:00                        
                  4                            1904-01-01 09:10:00                        
                  5                                                 1904-01-02 01:00:00   
                  6                                                 1904-01-02 01:10:00   
                  
                  Place                   Jim  
                  Number                       
                  2                            
                  3                            
                  4                            
                  5       1904-01-02 02:00:00  
                  6       1904-01-02 02:10:00  
                  

                  这篇关于按时间值进行透视排序 - Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 pandas 数据框中对最大和最小时间戳进行分组 下一篇:pandas :从数据透视表中的另一列中减去一列

                  相关文章

                    <legend id='0Lm9p'><style id='0Lm9p'><dir id='0Lm9p'><q id='0Lm9p'></q></dir></style></legend>

                      <small id='0Lm9p'></small><noframes id='0Lm9p'>

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