Pandas join/merge/concat two dataframes

前端 未结 2 714
挽巷
挽巷 2020-11-30 08:50

I am having issues with joins in pandas and I am trying to figure out what is wrong. Say I have a dataframe x:



        
相关标签:
2条回答
  • 2020-11-30 09:15

    Does your index have duplicates x.index.is_unique? If so would explain the behavior you're seeing:

    In [16]: left
    Out[16]: 
                a
    2000-01-01  1
    2000-01-01  1
    2000-01-01  1
    2000-01-02  2
    2000-01-02  2
    2000-01-02  2
    
    In [17]: right
    Out[17]: 
                b
    2000-01-01  3
    2000-01-01  3
    2000-01-01  3
    2000-01-02  4
    2000-01-02  4
    2000-01-02  4
    
    In [18]: left.join(right)
    Out[18]: 
                a  b
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-01  1  3
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    2000-01-02  2  4
    
    0 讨论(0)
  • 2020-11-30 09:20

    It sounds like maybe you want pandas.concat? merge and join do, well, joins, which means they will give you something based around the Cartesian product of the two inputs, but it sounds like you just want to paste them together into one big table.

    Edit: did you try concat with axis=1? It seems to do what you're asking for:

    >>> print x
              A         B         C
    0  0.155614 -0.252148  0.861163
    1  0.973517  1.156465 -0.458846
    2  2.504356 -0.356371 -0.737842
    3  0.012994  1.785123  0.161667
    4  0.574578  0.123689  0.017598
    >>> print y
             A2        B2        C2
    0 -0.280993  1.278750 -0.704449
    1  0.140282  1.955322 -0.953826
    2  0.581997 -0.239829  2.227069
    3 -0.876146 -1.955199 -0.155030
    4 -0.518593 -2.630978  0.333264
    >>> print pandas.concat([x, y], axis=1)
              A         B         C        A2        B2        C2
    0  0.155614 -0.252148  0.861163 -0.280993  1.278750 -0.704449
    1  0.973517  1.156465 -0.458846  0.140282  1.955322 -0.953826
    2  2.504356 -0.356371 -0.737842  0.581997 -0.239829  2.227069
    3  0.012994  1.785123  0.161667 -0.876146 -1.955199 -0.155030
    4  0.574578  0.123689  0.017598 -0.518593 -2.630978  0.333264
    
    0 讨论(0)
提交回复
热议问题