Creating an empty MultiIndex

前端 未结 4 777
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 15:29

I would like to create an empty DataFrame with a MultiIndex before assigning rows to it. I already found that empty DataFrames

4条回答
  •  长情又很酷
    2021-02-01 16:11

    Using pd.MultiIndex.from_arrays allows for a slightly more concise solution when defining the index explicitly:

    import pandas as pd
    ind = pd.MultiIndex.from_arrays([[]] * 3, names=(u'one', u'two', u'three'))
    df = pd.DataFrame(columns=['alpha', 'beta'], index=ind)
    df.loc[('apple','banana','cherry'), :] = [4, 3]
    
                         alpha  beta
    one   two    three              
    apple banana cherry      4     3
    

提交回复
热议问题