adding a row to a MultiIndex DataFrame/Series

前端 未结 2 1100
悲哀的现实
悲哀的现实 2020-12-09 04:23

I was wondering if there is an equivalent way to add a row to a Series or DataFrame with a MultiIndex as there is with a single index, i.e. using .ix or .loc?

I thou

2条回答
  •  半阙折子戏
    2020-12-09 05:02

    Update since .ix is depreciated: Today you could do:

    # say you have dataframe x
    x
    Out[78]: 
                  a    b       time
    indA indB                     
    a    i      0.0  NaN 2018-09-12
    b    j      1.0  2.0 2018-10-12
    c    k      2.0  3.0 2018-11-12
         f      NaN  NaN        NaT
    d    i      5.0  NaN        NaT
    
    x.loc[('a','k'),:] = (3.5,6,pd.NaT)
    
    x
    Out[80]: 
                  a    b       time
    indA indB                     
    a    i      0.0  NaN 2018-09-12
    b    j      1.0  2.0 2018-10-12
    c    k      2.0  3.0 2018-11-12
         f      NaN  NaN        NaT
    d    i      5.0  NaN        NaT
    a    k      3.5  6.0        NaT
    

提交回复
热议问题