Combine two Series using concat():
import numpy as np
import pandas as pd
s1 = pd.Series(['p', 'q'])
s2 = pd.Series(['x', 'y'])
pd.concat([s1, s2])
Reset the above result by setting the ignore_index option to True:
pd.concat([s1, s2], ignore_index=True)
Add a hierarchical index at the outermost level of the data with the keys option:
pd.concat([s1, s2], keys=['s1', 's2'])
Label the index keys with the names option:
pd.concat([s1, s2], keys=['s1', 's2'],
names=['Series name', 'Row ID'])
Combine two DataFrame objects with identical columns:
df1 = pd.DataFrame([['p', 2], ['q', 3]],
columns=['letter', 'number'])
df1
df2 = pd.DataFrame([['r', 4], ['s', 5]],
columns=['letter', 'number'])
df2
pd.concat([df1, df2])
Combine DataFrame objects with overlapping columns and return everything. Columns outside the intersection will
be filled with NaN values.
df3 = pd.DataFrame([['r', 4, 'lion'], ['s', 5, 'fox']],
columns=['letter', 'number', 'animal'])
df3
pd.concat([df1, df3], sort=False)
Combine DataFrame objects with overlapping columns and return only those that are shared by passing inner
to the join keyword argument.
pd.concat([df1, df3], join="inner")
Combine DataFrame objects horizontally along the x axis by passing in axis=1:
df4 = pd.DataFrame([['hen', 'jack'], ['dog', 'jolly']],
columns=['animal', 'name'])
pd.concat([df1, df4], axis=1)
Prevent the result from including duplicate index values with the verify_integrity option:
df5 = pd.DataFrame([1], index=['p'])
df5
df6 = pd.DataFrame([2], index=['p'])
df6