Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧多索引重新索引列不工作_Python_Pandas_Dataframe_Reindex - Fatal编程技术网

Python 数据帧多索引重新索引列不工作

Python 数据帧多索引重新索引列不工作,python,pandas,dataframe,reindex,Python,Pandas,Dataframe,Reindex,我有一个数据框,列上有一个多索引 ipdb> actions flow inflow outflow action Investment Trade ExternalFee Fee date sequence 2016-10-18 50 15000.0 NaN

我有一个数据框,列上有一个多索引

ipdb> actions
flow                    inflow  outflow                   
action              Investment    Trade ExternalFee    Fee
date       sequence                                       
2016-10-18 50          15000.0      NaN         NaN    NaN
           55              NaN      NaN      -513.0    NaN
           60              NaN -14402.4         NaN    NaN
           70              NaN      NaN         NaN -14.29
我希望重新编制索引,从而添加和“收入”列

ipdb> actions.reindex(columns=['Investment', 'Trade', 'ExternalFee', 'Fee', 'Income'], level=1)
flow                    inflow  outflow                   
action              Investment    Trade ExternalFee    Fee
date       sequence                                       
2016-10-18 50          15000.0      NaN         NaN    NaN
           55              NaN      NaN      -513.0    NaN
           60              NaN -14402.4         NaN    NaN
           70              NaN      NaN         NaN -14.29
未添加“收入”列

我还尝试命名级别:

ipdb> actions.reindex(columns=['Investment', 'Trade', 'Income'], level='action')
flow                    inflow  outflow
action              Investment    Trade
date       sequence                    
2016-10-18 50          15000.0      NaN
           55              NaN      NaN
           60              NaN -14402.4
您需要按所有列排序-因此需要将
多索引导出到元组,添加值并最后重新索引:

tuples = actions.columns.tolist()
tuples = tuples + [('outflow','Income')]
print (tuples)
[('inflow', 'Investment'), ('outflow', 'Trade'), 
 ('outflow', 'ExternalFee'), ('outflow', 'Fee'), 
('outflow', 'Income')]

a = actions.reindex(columns=pd.MultiIndex.from_tuples(tuples))
print (a)
                  inflow  outflow                          
              Investment    Trade ExternalFee    Fee Income
2016-10-18 50    15000.0      NaN         NaN    NaN    NaN
           55        NaN      NaN      -513.0    NaN    NaN
           60        NaN -14402.4         NaN    NaN    NaN
           70        NaN      NaN         NaN -14.29    NaN

另一个可行的解决方案是:

actions[('outflow','Income')] = np.nan
print (actions)
action            inflow  outflow                          
date          Investment    Trade ExternalFee    Fee Income
2016-10-18 50    15000.0      NaN         NaN    NaN    NaN
           55        NaN      NaN      -513.0    NaN    NaN
           60        NaN -14402.4         NaN    NaN    NaN
           70        NaN      NaN         NaN -14.29    NaN

答案不够好,无法接受??