Nakor :
I have a pandas DataFrame with MultiIndex, and I'm looking for a fast way to modify a subset of one of my MultiIndex levels for some of the levels. Here is an example where I need to change the 2 indices (0, 10) and (9, 25) and change their "end" level.
import pandas as pd
# Make up some data
data = pd.DataFrame({
'start': [0, 12, 9, 24],
'end': [10, 20, 25, 32],
'col1': ['a', 'b', 'a', 'd'],
'col2': [1, 1, 2, 2]
}).set_index(['start', 'end'])
# Idx to change for the "end" level
idx_to_change = {(0, 10), (9, 25)}
### A cumbersome way to do it ###
data.reset_index(inplace=True)
subset = [True if (s, t) in idx_to_change else False for (s, t, _, _) in data.values]
data.loc[subset, 'end'] += 10
# Update the data
data.set_index(['start', 'end'], inplace=True)
As you can see, it takes a little bit of code to change some indices (and it might be especially slow). Do you know of a better way to do it?
Thank you for your help
Henry Yik :
One way is to re-assign the index by pd.MultiIndex
:
idx_to_change = {(0, 10), (9, 25)}
data.index = pd.MultiIndex.from_tuples([i if i not in idx_to_change else (i[0],i[1]+10) for i in data.index], names=("start","end"))
print (data)
col1 col2
start end
0 20 a 1
12 20 b 1
9 35 a 2
24 32 d 2