1

I have a list of dataframes, each has a multi-index. one column is a varName, the other is 'round'.

The values in the varName column are numbers. I have another dataframe that is a mapping of the numbers to labels. I want to use map() on the varName column, but since it's part of the index, there IS no varName column.

I've tried to copy the varname column, or make it not part of the index anymore, but none of these things seem to work.

2
  • how-to-make-good-reproducible-pandas-examples Commented May 31, 2016 at 21:04
  • Actually, the correct answer for this specific case turned out to be "do the mapping on the data before that column becomes the index." Which won't work in all cases, but works for this one... Commented Jun 2, 2016 at 15:48

2 Answers 2

1

If varName is the name of one of the MultiIndex levels, you should be able to:

df.reset_index(level='varName')

to convert varName to a column and then use map(). If varName is not the name, you should still be able to use level=0 (or 1).

Sign up to request clarification or add additional context in comments.

Comments

0

maybe you can do something like:

df.reset_index(level=1).merge(df2)

this is assuming level=1 is your common column (eg 'varName') between two dfs. if you wish you can than set_index for the label name, like:

df.reset_index(level=1).merge(df2).set_index(['labels'],append=True)

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.