1
df = pd.DataFrame(np.arange(4*3).reshape(4,3), index=[['a','a','b','b'],[1,2,1,2]], columns=list('xyz'))

where df looks like:

enter image description here

Now I add a new row by:

df.loc['new',:]=[0,0,0]

Then df becomes:

enter image description here

Now I want to do the same but with a different df that has non-unique multi-index:

df = pd.DataFrame(np.arange(4*3).reshape(4,3), index=[['a','a','b','b'],[1,1,2,2]], columns=list('xyz'))

,which looks like:

enter image description here

and call

df.loc['new',:]=[0,0,0]

The result is "Exception: cannot handle a non-unique multi-index!"

How could I achieve the goal?

Royalblue
  • 639
  • 10
  • 22

1 Answers1

1

Use append or concat with helper DataFrame:

df1 = pd.DataFrame([[0,0,0]], 
                   columns=df.columns, 
                   index=pd.MultiIndex.from_arrays([['new'], ['']]))
df2 = df.append(df1)

df2 = pd.concat([df, df1])
print (df2)
       x   y   z
a   1  0   1   2
    1  3   4   5
b   2  6   7   8
    2  9  10  11
new    0   0   0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    thank you so much for great help. I will click on the accept button in 10 minutes (system default) :) – Royalblue Sep 20 '18 at 09:22