Search code examples
python-3.xpandasdataframeseries

Add Multiindex Dataframe and corresponding Series


I am failing to add a multiindex dataframe and a corresponding series. E.g.,

df = pd.DataFrame({
    'a': [0, 0, 1, 1], 'b': [0, 1, 0, 1], 
    'c': [1, 2, 3, 4], 'd':[1, 1, 1, 1]}).set_index(['a', 'b'])
# Dataframe might contain records that are not in the series and vice versa
s = df['d'].iloc[1:]
df + s

produces

ValueError: cannot join with no overlapping index names

Does anyone know how to resolve this? I can work around the issue by adding each column separately, using e.g.

df['d'] + s

But I would like to add the two in a single operation. Any help is much appreciated.


Solution

  • By default, + tries to align along columns, the following would work with +:

    s = df.iloc[:, 1:]
    
    df + s
    
    #      c  d
    #a b       
    #0 0 NaN  2
    #  1 NaN  2
    #1 0 NaN  2
    #  1 NaN  2
    

    In your case, you need to align along index. You can explicitly specify axis=0 with add method for that:

    df.add(s, axis=0)
    
    #       c    d
    #a b          
    #0 0  NaN  NaN
    #  1  3.0  2.0
    #1 0  4.0  2.0
    #  1  5.0  2.0