9

I would like to merge two DataFrames while creating a multilevel column naming scheme denoting which dataframe the rows came from. For example:

In [98]: A=pd.DataFrame(np.arange(9.).reshape(3,3),columns=list('abc'))
In [99]: A
Out[99]: 
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8

In [100]: B=A.copy()

If I use pd.merge(), then I get

In [104]: pd.merge(A,B,left_index=True,right_index=True)
Out[104]: 
   a_x  b_x  c_x  a_y  b_y  c_y
0    0    1    2    0    1    2
1    3    4    5    3    4    5
2    6    7    8    6    7    8

Which is what I expect with that statement, what I would like (but I don't know how to get!) is:

In [104]: <<one or more statements>>
Out[104]: 
     A              B
     a    b    c    a    b    c
0    0    1    2    0    1    2
1    3    4    5    3    4    5
2    6    7    8    6    7    8

Can this be done without changing the original pd.DataFrame calls? I am reading the data in the dataframes in from .csv files and that might be my problem.

4

2 回答 2

7

第一种情况可以在 A、B 之间任意排序(不是列,只是 A 或 B 的顺序)第二种应该保持排序

恕我直言,这是恐慌!

In [5]: concat(dict(A = A, B = B),axis=1)
Out[5]: 
   A        B      
   a  b  c  a  b  c
0  0  1  2  0  1  2
1  3  4  5  3  4  5
2  6  7  8  6  7  8

In [6]: concat([ A, B ], keys=['A','B'],axis=1)
Out[6]: 
   A        B      
   a  b  c  a  b  c
0  0  1  2  0  1  2
1  3  4  5  3  4  5
2  6  7  8  6  7  8
于 2013-09-23T18:57:11.720 回答
6

这是一种方法,它确实改变了 A 和 B:

In [10]: from itertools import cycle

In [11]: A.columns = pd.MultiIndex.from_tuples(zip(cycle('A'), A.columns))

In [12]: A
Out[12]:
   A
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8

In [13]: B.columns = pd.MultiIndex.from_tuples(zip(cycle('B'), B.columns))

In [14]: A.join(B)
Out[14]:
   A        B
   a  b  c  a  b  c
0  0  1  2  0  1  2
1  3  4  5  3  4  5
2  6  7  8  6  7  8

我实际上认为这将是一个很好的替代行为,而不是后缀......

于 2013-09-23T17:23:50.327 回答