关于pandas里面的合并
from pandas import * from numpy import * import json from pylab import * left = DataFrame({'key1':['foo','foo','bar'],'key2':['one','two','one'],'lval':[1,2,3]}) right = DataFrame({'key1':['foo','foo','bar','bar'],'key2':['one','one','one','two'],'rval':[4,5,6,7]})
下一步
demo = merge(left,right,on=['key1','key2'],how='outer')
print(demo)
结果为:
key1 key2 lval rval
0 foo one 1.0 4.0
1 foo one 1.0 5.0
2 foo two 2.0 NaN
3 bar one 3.0 6.0
4 bar two NaN 7.0
可以看到结果是全的,列举了所有可能
demo = merge(left,right,on=['key1','key2'],how='left')
print(demo)
结果为:
key1 key2 lval rval
0 foo one 1 4.0
1 foo one 1 5.0
2 foo two 2 NaN
3 bar one 3 6.0
可以看到是以第一个表为判断依据,第二个表部分数据没有显示
demo = merge(left,right,on=['key1','key2'],how='right')
print(demo)
结果为:
key1 key2 lval rval
0 foo one 1.0 4
1 foo one 1.0 5
2 bar one 3.0 6
3 bar two NaN 7
可以看出与上一个整好相反