Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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_Series - Fatal编程技术网

Python 将两列数据帧转换为多索引系列

Python 将两列数据帧转换为多索引系列,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,如何打开此数据帧: In [1]: df Out[1]: col_name C P FULL count_x 1 5 count_y 2 6 CALIB count_x 3 7 count_y 4 8 在本系列中: In [2]: s Out[2]: C FULL count_x 1 count_y 2 CALIB count_x 3

如何打开此数据帧:

In [1]: df
Out[1]:
col_name         C    P
FULL  count_x    1    5
      count_y    2    6
CALIB count_x    3    7
      count_y    4    8
在本系列中:

In [2]: s
Out[2]:    
C  FULL  count_x    1
         count_y    2
   CALIB count_x    3
         count_y    4
P  FULL  count_x    5
         count_y    6
   CALIB count_x    7
         count_y    8
Python 3,Pandas 1.1.1.

两个级别都使用:

s = df.unstack([0,1])
print (s)
C  FULL      count_x    1
             count_y    2
   CALIB     count_x    3
             count_y    4
P  FULL      count_x    5
             count_y    6
   CALIB     count_x    7
             count_y    8
dtype: int64
另一个想法与另一个过程有关,但有必要进行另一个过程,并且:

尝试新事物

out = pd.concat({x : df[x] for x in df.columns})
Out[113]: 
   col_name  col_name1
C  FULL      count_x      1
             count_y      2
   CALIB     count_x      3
             count_y      4
P  FULL      count_x      5
             count_y      6
   CALIB     count_x      7
             count_y      8
dtype: int64

我总是忘了拆堆栈!
out = pd.concat({x : df[x] for x in df.columns})
Out[113]: 
   col_name  col_name1
C  FULL      count_x      1
             count_y      2
   CALIB     count_x      3
             count_y      4
P  FULL      count_x      5
             count_y      6
   CALIB     count_x      7
             count_y      8
dtype: int64