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.

Brian Postow
  • 11,709
  • 17
  • 81
  • 125
  • [how-to-make-good-reproducible-pandas-examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – MaxU - stand with Ukraine May 31 '16 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... – Brian Postow Jun 02 '16 at 15:48

2 Answers2

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).

Stefan
  • 41,759
  • 13
  • 76
  • 81
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)
Siraj S.
  • 3,481
  • 3
  • 34
  • 48