Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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_Excel_Pandas - Fatal编程技术网

Python 将多索引列合并到数据帧中的单个索引

Python 将多索引列合并到数据帧中的单个索引,python,excel,pandas,Python,Excel,Pandas,使用我的代码,我在1中集成了2个数据库。问题是,当我向数据库中再添加一列时,结果并不像预期的那样。使用Python2.7 代码: 档案1: Fecha Cliente Impresiones Impresiones 2 Revenue 20/12/17 Jose 1312 35 $12 20/12/17 Martin 12 56 $146 20/12/1

使用我的代码,我在1中集成了2个数据库。问题是,当我向数据库中再添加一列时,结果并不像预期的那样。使用Python2.7

代码:

档案1:

Fecha       Cliente     Impresiones Impresiones 2   Revenue
20/12/17    Jose        1312        35              $12
20/12/17    Martin      12          56              $146
20/12/17    Pedro       5443        124             $1,256
20/12/17    Esteban     667         1235            $1
档案2:

Fecha       Cliente     Impresiones Impresiones 2   Revenue
21/12/17    Jose        25          5               $2
21/12/17    Martin      6347        523             $123
21/12/17    Pedro       2368        898             $22
21/12/17    Esteban     235         99              $7,890
希望结果:


我试过使用
m1=df.index.get_level_值(1)='impressiones 2'df.index=np.where(m1,'impressiones 2',df.index.get_level_值(0))
,但我有一个错误:
索引器:级别太多:索引只有1级,而不是2级
解决方案的第一位与您之前的问题类似,使用
concat
+
设置索引
+
堆栈
+
取消堆栈
+
排序索引

df = pd.concat([df1, df2])\
       .set_index(['Cliente', 'Fecha'])\
       .stack()\
       .unstack(-2)\
       .sort_index(ascending=[True, False])
现在是挑战性的部分,我们必须将第0级中的名称合并到第1级中,然后重置索引

我使用
np.insert
在索引中收入条目上方插入名称

i, j = df.index.get_level_values(0), df.index.get_level_values(1)
k = np.insert(j.values, np.flatnonzero(j == 'Revenue'), i.unique())
现在,我创建了一个新的
多索引
,然后用它来
重新索引
df
-

idx = pd.MultiIndex.from_arrays([i.unique().repeat(len(df.index.levels[1]) + 1), k])
df = df.reindex(idx).fillna('')
现在,放下额外的关卡-

df.index = df.index.droplevel()

df

Fecha        20/12/17 21/12/17
Esteban                       
Revenue            $1   $7,890
Impresiones2     1235       99
Impresiones       667      235
Jose                          
Revenue           $12       $2
Impresiones2       35        5
Impresiones      1312       25
Martin                        
Revenue          $146     $123
Impresiones2       56      523
Impresiones        12     6347
Pedro                         
Revenue        $1,256      $22
Impresiones2      124      898
Impresiones      5443     2368
df.index = df.index.droplevel()

df

Fecha        20/12/17 21/12/17
Esteban                       
Revenue            $1   $7,890
Impresiones2     1235       99
Impresiones       667      235
Jose                          
Revenue           $12       $2
Impresiones2       35        5
Impresiones      1312       25
Martin                        
Revenue          $146     $123
Impresiones2       56      523
Impresiones        12     6347
Pedro                         
Revenue        $1,256      $22
Impresiones2      124      898
Impresiones      5443     2368