DataFrame索引合并 join

from pandas import DataFrame

left = DataFrame([[1,2],[3,4],[5,6]],index=['a','c','e'],columns=['item1','item2'])
right = DataFrame([[7,8],[9,10],[11,12],[13,14]],index=['b','c','d','e'],columns=['item3','item4'])

print(left)
'''
   item1  item2
a      1      2
c      3      4
e      5      6
'''
print(right)
'''
   item3  item4
b      7      8
c      9     10
d     11     12
e     13     14
'''
print(left.join(right))
'''
   item1  item2  item3  item4
a      1      2    NaN    NaN
c      3      4    9.0   10.0
e      5      6   13.0   14.0
'''
print(left.join(right,how='outer'))
'''
   item1  item2  item3  item4
a    1.0    2.0    NaN    NaN
b    NaN    NaN    7.0    8.0
c    3.0    4.0    9.0   10.0
d    NaN    NaN   11.0   12.0
e    5.0    6.0   13.0   14.0
'''

 

posted @ 2021-02-25 15:35  OTAKU_nicole  阅读(747)  评论(0编辑  收藏  举报