Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
import pandas as pd
import numpy as np
Concatenation
# NumPy Concatenation
x = [1,2,3]
y = [11,12,13]
z= [111,112,113]
np.concatenate([x,y,z])
array([ 1, 2, 3, 11, 12, 13, 111, 112, 113])
# Pandas Series Concatenation similar to np.concatenate
s1 = pd.Series(['A', 'B', 'C'], index=[1, 2, 3])
s2 = pd.Series(['D', 'E', 'F'], index=[4, 5, 6])
pd.concat([s1,s2])
1 A
2 B
3 C
4 D
5 E
6 F
dtype: object
# Pandas DataFrame Concatenantion
df1 = pd.DataFrame([['a1','b1'],['a2','b2']], index=[1,2], columns=['A','B'])
df2 = pd.DataFrame([['a3','b3'],['a4','b4']], index=[1,2], columns=['A','B'])
print('df1:\n',df1)
print('df2:\n',df2)
print('Concatenate df1 & df2:\n',pd.concat([df1, df2]))
df1:
A B
1 a1 b1
2 a2 b2
df2:
A B
1 a3 b3
2 a4 b4
Concatenate df1 & df2:
A B
1 a1 b1
2 a2 b2
1 a3 b3
2 a4 b4