blokeley
blokeley

Reputation: 7065

How to set index values in a MultiIndex pandas DataFrame?

I have a MultiIndex pandas DataFrame and need to change the level 0 index values. The current DataFrame has a MultiIndex called (test, time).

How do I set the string "Test 4" (in the top level of the index) to be some other string?

For extra points, many of my DataFrames will have more than one value in the test level of the index. Can I use a list of names to rename more than one at a time?

Thank you for any help.

enter image description here

(edited:) My data is:

test      time     force  displacement  minutes  event
'Test 4'  0.0000   .1202   0            0.00000  False
'Test 4'  0.0012   .0901   0            0.00002  False
'Test 4'  0.0018   .0901   0            0.00003  False
'Test 4'  0.0030   .0901   0            0.00004  False
'Test 4'  0.0042   .0901   0            0.00005  False
'Test 5'  0.0000   .1203   0            0.00000  False
'Test 5'  0.0012   .0901   0            0.00002  False

with a multi index set to ['test', 'time']

Upvotes: 3

Views: 4419

Answers (2)

Ted Petrou
Ted Petrou

Reputation: 62037

You can pass a dictionary to rename to rename specific values in any index.

df.rename(index={'Test4':'something else'})

Upvotes: 6

piRSquared
piRSquared

Reputation: 294506

First off, don't post a pic without the data?

Second of all:

df.index.set_levels(['Test4'], level=0, inplace=True)

Upvotes: 3

Related Questions