?分别测试了how为空(默认情况)inner,outer,left,right 5种情况,发现默认参数为inner
import pandas as pd
df1=pd.DataFrame({'key': list('bbacd'),'data1':range(5)})
df2=pd.DataFrame({'key': list('aabce'),'data2':range(5)})
print("df1:",df1,sep="\n")
print("df2",df2,sep="\n")
print("inner",pd.merge(df1,df2),sep="\n")
print("inner",pd.merge(df1,df2,how='inner'),sep="\n")
print("outer",pd.merge(df1,df2,how='outer'),sep="\n")
print("left",pd.merge(df1,df2,how='left'),sep="\n")
print("right",pd.merge(df1,df2,how='right'),sep="\n")
实验结果?
df1:
key data1
0 b 0
1 b 1
2 a 2
3 c 3
4 d 4
df2
key data2
0 a 0
1 a 1
2 b 2
3 c 3
4 e 4
inner
key data1 data2
0 b 0 2
1 b 1 2
2 a 2 0
3 a 2 1
4 c 3 3
inner
key data1 data2
0 b 0 2
1 b 1 2
2 a 2 0
3 a 2 1
4 c 3 3
outer
key data1 data2
0 b 0.0 2.0
1 b 1.0 2.0
2 a 2.0 0.0
3 a 2.0 1.0
4 c 3.0 3.0
5 d 4.0 NaN
6 e NaN 4.0
left
key data1 data2
0 b 0 2.0
1 b 1 2.0
2 a 2 0.0
3 a 2 1.0
4 c 3 3.0
5 d 4 NaN
right
key data1 data2
0 a 2.0 0
1 a 2.0 1
2 b 0.0 2
3 b 1.0 2
4 c 3.0 3
5 e NaN 4
|