1

I have a multiindexed pandas data frame like:

            col1        col2        col3        col4        col5
ix  iy                                                        
0   14       True          1           1          1           1
    25       True          1           1          1           1
    27       True          1           1          0           1
    28       True          1           1          1           0
    43       True          1           1          1           1
1   12       True          1           1          1           1
    38       True          1           1          1           1
2   0        True          1           1          0           1
    1        True          1           1          1           0

How I can change the index by adding a constant value to them? For example ix + 5 and iy + 10:

            col1        col2        col3        col4        col5
ix  iy                                                        
5   24       True          1           1          1           1
    35       True          1           1          1           1
    37       True          1           1          0           1
    38       True          1           1          1           0
    53       True          1           1          1           1
6   22       True          1           1          1           1
    48       True          1           1          1           1
7   10       True          1           1          0           1
    11       True          1           1          1           0 

1 Answer 1

1

You can recreate new MultiIndex, here with tuples and MultiIndex.from_tuples:

L = [(a + 5, b + 10) for a, b in df.index]
df = df.set_index(pd.MultiIndex.from_tuples(L, names=df.index.names))

print (df)
      col1  col2  col3  col4  col5
ix iy                              
5  24  True     1     1     1     1
   35  True     1     1     1     1
   37  True     1     1     0     1
   38  True     1     1     1     0
   53  True     1     1     1     1
6  22  True     1     1     1     1
   48  True     1     1     1     1
7  10  True     1     1     0     1
   11  True     1     1     1     0

Another idea with MultiIndex.set_levels:

df.index = df.index.set_levels(df.index.levels[0] + 5, level=0)
df.index = df.index.set_levels(df.index.levels[1] + 10, level=1)
print (df)
       col1  col2  col3  col4  col5
ix iy                              
5  24  True     1     1     1     1
   35  True     1     1     1     1
   37  True     1     1     0     1
   38  True     1     1     1     0
   53  True     1     1     1     1
6  22  True     1     1     1     1
   48  True     1     1     1     1
7  10  True     1     1     0     1
   11  True     1     1     1     0
Sign up to request clarification or add additional context in comments.

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.