简体   繁体   中英

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:

        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

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

You passed inplace=True so it's performed inplace surprisingly enough so returns nothing when you assigned to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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