0

With below code I tried to insert a new row label (two name-levels) but it appears that MultiIndex.insert() does not work according to the printed output. User xyzjayne mentions here "MultiIndex is immutable". Then why is there MultiIndex.insert and/or how to use MultiIndex.insert properly in below case to get desired result?

NOT an option:

add ('D', '9') to labels tuple list before MultiIndexing. This labels is general, has a default list order and is used a few times elsewhere in different settings. And thus prior to the creation of a DataFrame.

Versions :pandas 0.25.1 py37ha925a31_0

Desired result:

row_labels MultiIndex([('A', '1'),
                       ('A', '2'),
                       ('B', '3'),
                       ('B', '4'),
                       ('C', '5'),
                       ('C', '6'),
                       ('D', '9'),   # inserted row label ('D', '9')
                       ('D', '7'),
                       ('D', '8')],
                       names=['group', 'subgroup'])

My code:

import pandas as pd

labels = {'A' : ['1', '2'],
          'B' : ['3', '4'],
          'C' : ['5', '6'],
          'D' : ['7', '8']}

tpl = []

for g, sg in labels.items():
  for s in sg:
    tpl.append((g, s))

row_labels = pd.MultiIndex.from_tuples(tpl, names=['group', 'subgroup'])

print ('row_labels', row_labels)

i = row_labels.get_locs(('D',))

print ('\nD is found at row(s): ', i)

# add row at position 6
row_labels.insert(6, (('D', '9')))

# check for added label
print ('\nrow_labels', row_labels)

My current output:

row_labels MultiIndex([('A', '1'),
                       ('A', '2'),
                       ('B', '3'),
                       ('B', '4'),
                       ('C', '5'),
                       ('C', '6'),
                       ('D', '7'),
                       ('D', '8')],
                       names=['group', 'subgroup'])

D is found at row(s):  [6 7]

row_labels MultiIndex([('A', '1'),
                       ('A', '2'),
                       ('B', '3'),
                       ('B', '4'),
                       ('C', '5'),
                       ('C', '6'),
                       ('D', '7'),
                       ('D', '8')],
                       names=['group', 'subgroup'])

2 Answers 2

1

You can assign back, Index.insert not working inplace:

print (pd.__version__)
0.25.1

# add row at position 6
row_labels = row_labels.insert(6, (('D', '9')))

# check for added label
print ('\nrow_labels', row_labels)
row_labels MultiIndex([('A', '1'),
            ('A', '2'),
            ('B', '3'),
            ('B', '4'),
            ('C', '5'),
            ('C', '6'),
            ('D', '9'),
            ('D', '7'),
            ('D', '8')],
           names=['group', 'subgroup'])
Sign up to request clarification or add additional context in comments.

3 Comments

.. I just figured # add row at position 6 row_labels = pd.MultiIndex.insert(row_labels, 6, ('D', '9')) out... but still in docs pandas 0.19 it mentions MultiIndex (loc, items).
@ZF007 - My opinion - this function is used very rare, so maybe buggy. Obviously is MultiIndex converted to list of tuples or to DataFrames, procesing and then convert back to Multiindex.
Yeah.. buggy indeed. Thanks for jumping in. It saves me an hour or two of headachs on this.
0

As you can see from the discussion with jezrael the .insert appears to be sloppy made or missing text at the docs.

My alternative also works:

row_labels = pd.MultiIndex.insert(row_labels, 6, ('D', '9'))

And is not as such described here at the pandas docs 0.25.0 .

Here at 0.19.0 it is and the text should be:

MultiIndex.insert(index, loc, item)

and thus missing the previous made "MultiIndex" as "Index".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.