set multi index of an existing data frame in pandas

后端 未结 1 1848
说谎
说谎 2021-01-30 16:02

I have a DataFrame that looks like

  Emp1    Empl2           date       Company
0    0        0     2012-05-01         apple
1    0        1     201         


        
相关标签:
1条回答
  • 2021-01-30 16:52

    When you pass inplace in makes the changes on the original variable and returns None, and the function does not return the modified dataframe, it returns None.

    is_none = df.set_index(['Company', 'date'], inplace=True)
    df  # the dataframe you want
    is_none # has the value None
    

    so when you have a line like:

    df = df.set_index(['Company', 'date'], inplace=True)
    

    it first modifies df... but then it sets df to None!

    That is, you should just use the line:

    df.set_index(['Company', 'date'], inplace=True)
    
    0 讨论(0)
提交回复
热议问题