简体   繁体   中英

Map multi-index column to new values in Pandas DataFrame

I have a multi-index dataframe in Pandas with two index values like this:

week_of_year    day_of_week

 1              0
 1              1
 .              .
 1              6

 2              0
 .              .
 .              .
 2              6
 .              .

I want to map the day_of_week index to {'0': 'Monday', '1': 'Tuesday', ...} but not sure how to access and map the second index.

Use Index.get_level_values :

d = {0: 'Monday', 1: 'Tuesday', ..., 6: 'Sunday'}

df.index.get_level_values(level='day_of_week').map(d)

output:

Index(['Monday', 'Tuesday', 'Sunday', 'Monday', 'Sunday'], dtype='object', name='day_of_week')

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