Creating an empty MultiIndex

前端 未结 4 744
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  猫巷女王i
    2021-02-01 15:51

    Another solution which is maybe a little simpler is to use the function set_index:

    >>> import pandas as pd
    >>> df = pd.DataFrame(columns=['one', 'two', 'three', 'alpha', 'beta'])
    >>> df = df.set_index(['one', 'two', 'three'])
    >>> df
    Empty DataFrame
    Columns: [alpha, beta]
    Index: []
    >>> df.loc[('apple','banana','cherry'),:] = [0.1, 0.2]
    >>> df
                        alpha beta
    one   two    three            
    apple banana cherry   0.1  0.2
    

提交回复
热议问题