concat
import pandas as pd
df1 = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]})
df2 = pd.DataFrame({‘A’: [7, 8, 9], ‘B’: [10, 11, 12]})
result = pd.concat([df1, df2]) # 在行上连接
merge
import pandas as pd
df1 = pd.DataFrame({‘key’: [‘A’, ‘B’, ‘C’], ‘value1’: [1, 2, 3]})
df2 = pd.DataFrame({‘key’: [‘B’, ‘C’, ‘D’], ‘value2’: [4, 5, 6]})
result = pd.merge(df1, df2, on=‘key’, how=‘inner’) # 内连接
join
import pandas as pd
df1 = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]}, index=[‘X’, ‘Y’, ‘Z’])
df2 = pd.DataFrame({‘C’: [7, 8, 9], ‘D’: [10, 11, 12]}, index=[‘X’, ‘Y’, ‘Z’])
result = df1.join(df2) # 使用索引连接
append
import pandas as pd
df1 = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]})
df2 = pd.DataFrame({‘A’: [7, 8, 9], ‘B’: [10, 11, 12]})
result = df1.append(df2, ignore_index=True) # 忽略索引,按顺序连接