import pandas as pd
import numpy as np
# concatenating
df1 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.ones((3, 4))*1, columns=['a', 'b', 'c', 'd'])
df3 = pd.DataFrame(np.ones((3, 4))*2, columns=['a', 'b', 'c', 'd'])
print(df1)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
print(df2)
# a b c d
# 0 1.0 1.0 1.0 1.0
# 1 1.0 1.0 1.0 1.0
# 2 1.0 1.0 1.0 1.0
print(df3)
# a b c d
# 0 2.0 2.0 2.0 2.0
# 1 2.0 2.0 2.0 2.0
# 2 2.0 2.0 2.0 2.0
print("**********concat 上下(左右)合并**********")
# axis = 0 指定行,表示 上下合并
# axis = 1 指定列,表示 左右合并
# 指定ignore_index = True,表示忽略原有行号,生成新行号
res = pd.concat([df1, df2, df3], axis=0, ignore_index=True)
print(res)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
# 0 1.0 1.0 1.0 1.0
# 1 1.0 1.0 1.0 1.0
# 2 1.0 1.0 1.0 1.0
# 0 2.0 2.0 2.0 2.0
# 1 2.0 2.0 2.0 2.0
# 2 2.0 2.0 2.0 2.0
print("**********concat的参数 join 合并**********")
# axis = 0 指定行,表示 上下合并
# axis = 1 指定列,表示 左右合并
# join = {'inner', 'outer'}
df4 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd'])
df5 = pd.DataFrame(np.ones((3, 4))*1, columns=['b', 'c', 'd', 'e'])
print(df4)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
print(df5)
# b c d e
# 0 1.0 1.0 1.0 1.0
# 1 1.0 1.0 1.0 1.0
# 2 1.0 1.0 1.0 1.0
print("=======参数join的 outer上下(左右)合并方式=========")
# 会报警告,Python 3.7需要加上参数sort=False,就不会报警 默认是 join='outer' 链接,
res = pd.concat([df4, df5], join='outer', sort=False,ignore_index=True)
print(res)
# a b c d e
# 0 0.0 0.0 0.0 0.0 NaN
# 1 0.0 0.0 0.0 0.0 NaN
# 2 0.0 0.0 0.0 0.0 NaN
# 3 NaN 1.0 1.0 1.0 1.0
# 4 NaN 1.0 1.0 1.0 1.0
# 5 NaN 1.0 1.0 1.0 1.0
print("=======参数join的 inner 合并方式=========")
res = pd.concat([df4, df5], join='inner',ignore_index=True)
print(res)
# b c d
# 0 0.0 0.0 0.0
# 1 0.0 0.0 0.0
# 2 0.0 0.0 0.0
# 3 1.0 1.0 1.0
# 4 1.0 1.0 1.0
# 5 1.0 1.0 1.0
print("**********concat的参数 join_axes 合并**********")
# axis = 0 指定行,表示 上下合并
# axis = 1 指定列,表示 左右合并
df6 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd'],index=[1, 2, 3])
df7 = pd.DataFrame(np.ones((3, 4))*1, columns=['b', 'c', 'd', 'e'],index=[2, 3, 4])
print("===========左右合并时,行号不一致============")
# join_axes (指定哪个df为左表)指定按照哪一个df的行号进行合并
# 指定参数 join_axes时,类似于按照指定的左表进行左连接
res = pd.concat([df6, df7], axis=1, join_axes=[df7.index])
print(res)
# a b c d b c d e
# 1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN
# 2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
# 3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
# 不指定参数 join_axes时,类似于全连接
res = pd.concat([df6, df7], axis=1)
print(res)
# a b c d b c d e
# 1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN
# 2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
# 3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
# 4 NaN NaN NaN NaN 1.0 1.0 1.0 1.0
print("**********concat的参数 append 合并**********")
# axis = 0 指定行,表示 上下合并
# axis = 1 指定列,表示 左右合并
df8 = pd.DataFrame(np.ones((3, 4))*0, columns=['a', 'b', 'c', 'd'])
df9 = pd.DataFrame(np.ones((3, 4))*1, columns=['a', 'b', 'c', 'd'])
df10 = pd.DataFrame(np.ones((3, 4))*3, columns=['a', 'b', 'c', 'd'])
print(df8)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
print(df9)
# b c d e
# 0 1.0 1.0 1.0 1.0
# 1 1.0 1.0 1.0 1.0
# 2 1.0 1.0 1.0 1.0
print(df10)
# a b c d
# 0 3.0 3.0 3.0 3.0
# 1 3.0 3.0 3.0 3.0
# 2 3.0 3.0 3.0 3.0
print("==========连接两个 df=============")
# 不指定参数 sort=False 时会报警
# df9 追加到 df8 后面
res = df8.append(df9, ignore_index=True, sort=False)
print(res)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
# 3 1.0 1.0 1.0 1.0
# 4 1.0 1.0 1.0 1.0
# 5 1.0 1.0 1.0 1.0
print("============连接多个df============")
res = df8.append([df9, df10], ignore_index=True, sort=False)
print(res)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
# 3 1.0 1.0 1.0 1.0
# 4 1.0 1.0 1.0 1.0
# 5 1.0 1.0 1.0 1.0
# 6 3.0 3.0 3.0 3.0
# 7 3.0 3.0 3.0 3.0
# 8 3.0 3.0 3.0 3.0
print("===========append添加Series=============")
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
res = df8.append(s1, ignore_index=True)
print(res)
# a b c d
# 0 0.0 0.0 0.0 0.0
# 1 0.0 0.0 0.0 0.0
# 2 0.0 0.0 0.0 0.0
# 3 1.0 2.0 3.0 4.0