简体   繁体   中英

Changing values in a pandas dataframe multiindex

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM