Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 在python中重新构造表_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 3.x 在python中重新构造表

Python 3.x 在python中重新构造表,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有两个表,它们是数据帧对象(熊猫),如下所示: Table 1 Table 2 col1 col2 col3 col1 col2 col3 A |a1| 1 A |b1| 2 |a2| 3 |b2| 1 |a3| 2 B |b1| 1 B |a1| 1 |b2| 2 |a2| 5

我有两个表,它们是数据帧对象(熊猫),如下所示:

   Table 1            Table 2
 col1 col2 col3     col1  col2  col3 
   A  |a1|   1        A   |b1|  2
      |a2|   3            |b2|  1
      |a3|   2        B   |b1|  1
   B  |a1|   1            |b2|  2
      |a2|   5        C   |b1|  2
   C  |a1|   0            |b2|  0       
      |a2|   3
      |a3|   4
我想创建如下内容:

  a1  a2  a3  b1  b2
A  1   3   2   2   1
B  1   5   0   1   2
C  0   3   4   2   0
我认为你需要:


最后,如果需要删除列,请添加索引名:

df1 = pd.DataFrame({'col3': [1, 3, 2, 1, 5, 0, 3, 4], 
                    'col2': ['a1', 'a2', 'a3', 'a1', 'a2', 'a1', 'a2', 'a3'], 
                    'col1': ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'C']})
df1 = df1.set_index(['col1','col2'])
print (df1)
           col3
col1 col2      
A    a1       1
     a2       3
     a3       2
B    a1       1
     a2       5
C    a1       0
     a2       3
     a3       4

df2 = pd.DataFrame({'col3': [2, 1, 1, 2, 2, 0], 
                    'col2': ['b1', 'b2', 'b1', 'b2', 'b1', 'b2'], 
                    'col1': ['A', 'A', 'B', 'B', 'C', 'C']})
df2 = df2.set_index(['col1','col2'])
print (df2)
           col3
col1 col2      
A    b1       2
     b2       1
B    b1       1
     b2       2
C    b1       2
     b2       0
df = pd.concat([df1, df2])['col3'].unstack(fill_value=0)
print (df)
col2   a1 a2 a3 b1 b2
col1                 
A       1  3  2  2  1
B       1  5  0  1  2
C       0  3  4  2  0
df = pd.concat([df1, df2])['col3'] \
       .unstack(fill_value=0) \
       .rename_axis(None) \
       .rename_axis(None, axis=1)
print (df)
   a1  a2  a3  b1  b2
A   1   3   2   2   1
B   1   5   0   1   2
C   0   3   4   2   0