python – 替换pandas多索引中的值

我有一个带有多索引的数据框.我想在满足第一个索引的某些条件时更改第二个索引的值.

我在这里发现了一个类似(但不同)的问题:
Replace a value in MultiIndex (pandas)

这没有回答我的观点,因为那是关于改变单行,并且解决方案也传递了第一个索引的值(不需要更改).在我的情况下,我正在处理多行,我无法使该解决方案适应我的情况.

我的数据的最小示例如下.谢谢!

import pandas as pd
import numpy as np

consdf=pd.DataFrame()

for mylocation in ['North','South']:
    for scenario in np.arange(1,4):
        df= pd.DataFrame()
        df['mylocation'] = [mylocation]
        df['scenario']= [scenario]
        df['this'] = np.random.randint(10,100)
        df['that'] = df['this']  * 2
        df['something else']  = df['this'] * 3
        consdf=pd.concat((consdf, df ), axis=0, ignore_index=True)

mypiv = consdf.pivot('mylocation','scenario').transpose()

level_list =['this','that']
# if level 0 is in level_list --> set level 1 to np.nan
mypiv.iloc[mypiv.index.get_level_values(0).isin(level_list)].index.set_levels([np.nan], level =1, inplace=True)

最后一行不起作用:我得到:

ValueError: On level 1, label max (2) >= length of level  (1). NOTE: this index is in an inconsistent state

最佳答案 IIUC您可以使用
advanced indexing,
get_level_values,
set_levels
set_labels方法为级别值添加新值,然后更改索引的标签:

len_ind = len(mypiv.loc[(level_list,)].index.get_level_values(1))
mypiv.index.set_levels([1, 2, 3, np.nan], level=1, inplace=True)
mypiv.index.set_labels([3]*len_ind + mypiv.index.labels[1][len_ind:].tolist(), level=1, inplace=True)

In [219]: mypiv
Out[219]: 
mylocation               North  South
               scenario              
this           NaN          26     46
               NaN          32     67
               NaN          75     30
that           NaN          52     92
               NaN          64    134
               NaN         150     60
something else  1.0         78    138
                2.0         96    201
                3.0        225     90

注意其他方案的值将转换为float,因为它应该是一种类型,而np.nan具有float类型.

点赞