简体   繁体   English

从dataframe pandas创建多索引

[英]create multiindex from dataframe pandas

I'm trying to create the multi-index dataframe from following dataframe: 我正在尝试从以下数据帧创建多索引数据框:

               date_time                   session
              2015-07-30 10:32:54.000        1
              2015-07-30 10:32:54.000        1
              2015-07-30 10:36:39.000        1            
              2015-07-30 10:36:39.000        1
             ........................        1
              2015-07-30 11:58:57.000        2
              2015-07-30 12:18:37.000        2
              2015-07-30 12:28:51.000        2

to obtain smth like: 获得smth像:

        date_time                   session
      2015-07-30 10:32:54.000        1
      2015-07-30 10:32:54.000        
      2015-07-30 10:36:39.000                   
      2015-07-30 10:36:39.000        
     ........................        
      2015-07-30 11:58:57.000        2
      2015-07-30 12:18:37.000        
      2015-07-30 12:28:51.000        
      .......................        3

guiding by the answers for this question: MultiIndex Group By in Pandas Data Frame 引导这个问题的答案: 在熊猫数据框架中的MultiIndex Group By

I tried this code for my data: 我为我的数据尝试了这段代码:

def create_multi():
    multi=df.set_index(['session', 'date'], inplace=True)
    print multi

but it returns None I don't know if this method is appropriate for what I need to do and I just use it not correctly, or I should use another method 但是它返回None我不知道这个方法是否适合我需要做的事情而且我只是不正确地使用它,或者我应该使用另一种方法

You passed inplace=True so it's performed inplace surprisingly enough so returns nothing when you assigned to multi . 你传递了inplace=True所以它的执行位置足够令人惊讶,所以当你分配到multi时,什么都不返回。

def create_multi():
    multi=df.set_index(['session', 'date'], inplace=False)
    print multi

The above would work, check the docs , note that the default is inplace=False so it's strictly not necessary to specify if you want that behaviour 上面的方法可行,检查文档 ,注意默认值是inplace=False所以严格来说没有必要指定你是否需要这种行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM