pandas.concat
pandas.concat
(objs: Union[Iterable[Union[ForwardRef('DataFrame'), ForwardRef('Series')]], Mapping[Union[Hashable, NoneType], Union[ForwardRef('DataFrame'), ForwardRef('Series')]]], axis=0, join='outer', ignore_index: bool = False, keys=None, levels=None, names=None, verify_integrity: bool = False, sort: bool = False, copy: bool = True) → Union[ForwardRef('DataFrame'), ForwardRef('Series')][source]
Concatenate pandas objects along a particular axis with optional set logic along the other axes.
Can also add a layer of hierarchical indexing on the concatenation axis, which may be useful if the labels are the same (or overlapping) on the passed axis number.Parametersobjsa sequence or mapping of Series or DataFrame objects
If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised.axis{0/’index’, 1/’columns’}, default 0
The axis to concatenate along.join{‘inner’, ‘outer’}, default ‘outer’
How to handle indexes on other axis (or axes).ignore_indexbool, default False
If True, do not use the index values along the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join.keyssequence, default None
If multiple levels passed, should contain tuples. Construct hierarchical index using the passed keys as the outermost level.levelslist of sequences, default None
Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys.nameslist, default None
Names for the levels in the resulting hierarchical index.verify_integritybool, default False
Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation.sortbool, default False
Sort non-concatenation axis if it is not already aligned when join is ‘outer’. This has no effect when join='inner'
, which already preserves the order of the non-concatenation axis.
New in version 0.23.0.
Changed in version 1.0.0: Changed to not sort by default.copybool, default True
If False, do not copy data unnecessarily.Returnsobject, type of objs
When concatenating all Series
along the index (axis=0), a Series
is returned. When objs
contains at least one DataFrame
, a DataFrame
is returned. When concatenating along the columns (axis=1), a DataFrame
is returned.
See alsoSeries.append
Concatenate Series.DataFrame.append
Concatenate DataFrames.DataFrame.join
Join DataFrames using indexes.DataFrame.merge
Merge DataFrames by indexes or columns.
Notes
The keys, levels, and names arguments are all optional.
A walkthrough of how this method fits in with other tools for combining pandas objects can be found here.
Examples
Combine two Series
.
Clear the existing index and reset it in the result by setting the ignore_index
option to True
.
Add a hierarchical index at the outermost level of the data with the keys
option.
Label the index keys you create with the names
option.
Combine two DataFrame
objects with identical columns.
Combine DataFrame
objects with overlapping columns and return everything. Columns outside the intersection will be filled with NaN
values.
Combine DataFrame
objects with overlapping columns and return only those that are shared by passing inner
to the join
keyword argument.
Combine DataFrame
objects horizontally along the x axis by passing in axis=1
.
Prevent the result from including duplicate index values with the verify_integrity
option.
Reference : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html#pandas.concat
Last updated