简体   繁体   中英

Pandas: rename a index label in multiindex dataframe if NaN

How can I replace the inner index label of the df below?

Question                                 profile_gender. Gender           
Values                                    Male     Female
Question                    Values                                  
XCS_3. Think of a problem?  NaN        25.192318  25.900784

to:

Question                                 profile_gender. Gender           
Values                                    Male     Female
Question                    Values                                  
XCS_3. Think of a problem?  Okay Done   25.192318  25.900784

My failed attempt:

df.index = df.index.set_levels(['Okay Done'], 1)

Does nothing, doesn't even throw an error code

Looks like I have to replace the entire index which is very annoying.

df.index = pd.MultiIndex.from_tuples([('XCS_3. Think of a problem?', ' Okay Done')],
                                     names=['Question', 'Values'])

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