concatenate
英 /kən’kætɪneɪt/ 美 /kɑn’kætə,net/
adj. 连接的,连结的,连锁的
v. 连接,连结,使连锁
concat(objs, axis=0, join=‘outer’, join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=None, copy=True)
参数 | 含义 |
---|---|
objs | a sequence or mapping of Series, DataFrame, or Panel objects(论学好英语的重要性)是一个序列,其元素是向量或者矩阵 |
axis | 这个就是很迷,{0/‘index’, 1/‘columns’},The axis to concatenate along |
join | {‘inner’, ‘outer’}How to handle indexes on other axis(es) |
join_axes | 决定concat方向上保留哪些标签,与axis配合使用 |
ignore_index | If True, do not use the index values along the concatenation axis. The resulting axis will be labeled 0, …, n - 1. |
objs | |
objs |
确定拼接方向
import pandas as pd
import numpy as np
d1 = pd.DataFrame(np.random.rand(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
d2 = pd.DataFrame(np.arange(1, 13).reshape(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
print(pd.concat([d1, d2], axis=1)) # 列
print(pd.concat([d1, d2], axis=0)) # 行
是否保留原有的index方向的标签
import pandas as pd
import numpy as np
d1 = pd.DataFrame(np.random.rand(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
d2 = pd.DataFrame(np.arange(1, 13).reshape(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
print(pd.concat([d1, d2], ignore_index=True))
关于数据库里的内连接、左连接、右连接、全连接
import pandas as pd
import numpy as np
d1 = pd.DataFrame(np.random.rand(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
d2 = pd.DataFrame(np.arange(1, 13).reshape(4, 3), ['b', 'c', 'd', 'e'], [3, 4, 5])
print(pd.concat([d1, d2]))
print(pd.concat([d1, d2], join='inner'))
表示concat的轴上保留哪些label,与axis配个使用
import pandas as pd
import numpy as np
d1 = pd.DataFrame(np.random.rand(4, 3), ['a', 'b', 'c', 'd'], [1, 2, 3])
d2 = pd.DataFrame(np.arange(1, 13).reshape(4, 3), ['b', 'c', 'd', 'e'], [3, 4, 5])
print(pd.concat([d1, d2], axis=1, join_axes=[d1.index]))
print(pd.concat([d1, d2], join_axes=[d1.columns]))