代码之家  ›  专栏  ›  技术社区  ›  Charles R

添加缺少的行

  •  1
  • Charles R  · 技术社区  · 6 年前

    我有以下意见:

    Year    Brand   Model   Value
    2018    A           a   1,00
    2018    A           b   2,00
    2018    B           a   3,00
    2017    A           b   4,00
    2016    C           b   5,00
    

    我想添加缺少的组合:

    • 对于每个品牌,我必须有a和b

    Year    Brand   Model   Value
    2018    A          a    1
    2018    A          b    2
    2018    B          a    3,00
    2018    B          b    
    2018    C          a    
    2018    C          b    
    2017    A          a    
    2017    A          b    4
    2017    B          a    
    2017    B          b    
    2017    C          a    
    2017    C          b    
    2016    A          a    
    2016    A          b    
    2016    B          a    
    2016    B          b    
    2016    C          a    
    2016    C          b    5
    

    1 回复  |  直到 6 年前
        1
  •  3
  •   jezrael    6 年前

    reindex MultiIndex 创建人 MultiIndex.from_product

    mux = pd.MultiIndex.from_product([df['Year'].unique(),
                                      df['Brand'].unique(),
                                      df['Model'].unique()], names=['Year','Brand','Model'])
    df = df.set_index(['Year','Brand','Model']).reindex(mux).reset_index()
    print (df)
        Year Brand Model Value
    0   2018     A     a  1,00
    1   2018     A     b  2,00
    2   2018     B     a  3,00
    3   2018     B     b   NaN
    4   2018     C     a   NaN
    5   2018     C     b   NaN
    6   2017     A     a   NaN
    7   2017     A     b  4,00
    8   2017     B     a   NaN
    9   2017     B     b   NaN
    10  2017     C     a   NaN
    11  2017     C     b   NaN
    12  2016     A     a   NaN
    13  2016     A     b   NaN
    14  2016     B     a   NaN
    15  2016     B     b   NaN
    16  2016     C     a   NaN
    17  2016     C     b  5,00