Pandas: Multilevel column names

前端 未结 5 1133
梦毁少年i
梦毁少年i 2020-12-08 02:28

pandas has support for multi-level column names:

>>>  x = pd.DataFrame({\'instance\':[\'first\',\'first\',\'first\'],\'foo\':[\'a\',\'b         


        
5条回答
  •  难免孤独
    2020-12-08 03:14

    No need to create a list of tuples

    Use: pd.MultiIndex.from_product(iterables)

    import pandas as pd
    import numpy as np
    
    df = pd.Series(np.random.rand(3), index=["a","b","c"]).to_frame().T
    df.columns = pd.Multiindex.from_product([["new_label"], df.columns])
    

    Resultant DataFrame:

      new_label                    
              a         b         c
    0   0.25999  0.337535  0.333568
    

    Pull request from Jan 25, 2014

提交回复
热议问题