Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] Python:Pandas built-in-functions: .concat() #4814

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Applied changes and suggestions
  • Loading branch information
Liam-Dupeyron committed Jun 22, 2024
commit 78738918edbb88a021a832d9258a88f6420088c0
52 changes: 27 additions & 25 deletions content/pandas/concepts/built-in-functions/terms/concat/concat.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,50 @@
Title: '.concat()'
Description: 'Concatenates multiple Dataframes or Series along a particular axis'
Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR!
- 'A subject name'
- 'A second subject name'
- 'An nth subject name'
- 'Computer Science'
- 'Data Science'
Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR!
- 'Data Structures'
- 'Functions'
- 'Pandas'
- 'CSV'
- 'Data'
CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first
- 'learn-Data-Analysis-with-Pandas'
- 'paths/data-science'
- 'learn-python-3'
- 'paths/computer-science'
- 'paths/data-science-foundations'
---

The `.concat()` function is used to concatenate and combine multiple [`DataFrames`](https://www.codecademy.com/resources/docs/pandas/dataframe) or `Series` along a particular axis.
The **`.concat()`** function is used to concatenate and combine multiple [`DataFrames`](https://www.codecademy.com/resources/docs/pandas/dataframe) or `Series` along a particular axis.

## Syntax

```pseudo
pandas.concat(objs)
pandas.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
```

The `objs` parameter is essential and must be specified. It represents the objects to be concatenated and can be a sequence or mapping of pandas Series or DataFrame objects. The objects should be passed as a sequence (like a list or tuple) or a mapping (like a dictionary).

The rest of the parameters are listed below:

| Parameter Name | Data Type | Usage |
| :----------------: | :--------------------------------------------------: | -------------------------------------------------------------------------------------- |
| `axis` | int (0 for rows, 1 for columns ), default 0 | Specifies the axis along which to concatenate the objects |
| `join` | str (“outer” (default), “inner,” “left,” or “right”) | Determines how to handle indexes on other axes |
| `ignore_index` | bool, default `False` | If `True`, it resets the index in the resulting DataFrame |
| `keys` | sequence (list or tuple), default None | Lets you construct a hierarchical index using the provided keys as the outermost level |
| `levels` | list of sequences, default None | Specific levels to use for constructing a MultiIndex if keys are provided |
| `names` | list of str, default None | Names for the levels generated in the hierarchical index |
| `verify_integrity` | bool, default `False` | If `True`, checks whether the new concatenated axis contains duplicates |
| `sort` | bool, default `False` | If `True`, it sorts the resulting Series or Dataframe by the keys |
| `copy` | bool, default `True` | If `False`, it avoids copying data unnecessarily |
| Parameter Name | Data Type | Usage |
| :----------------: | :--------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------------------ |
| `objs` | list, tuple, dict, Series, DataFrame | The primary argument where you pass the sequence or mapping of pandas Series or DataFrame objects that you want to concatenate |
| `axis` | int (0 for rows, 1 for columns ), default 0 | Specifies the axis along which to concatenate the objects |
| `join` | str (“outer” (default), “inner,” “left,” or “right”) | Determines how to handle indexes on other axes |
| `ignore_index` | bool, default `False` | If `True`, it resets the index in the resulting DataFrame |
| `keys` | sequence (list or tuple), default None | Lets you construct a hierarchical index using the provided keys as the outermost level |
| `levels` | list of sequences, default None | Specific levels to use for constructing a MultiIndex if keys are provided |
| `names` | list of str, default None | Names for the levels generated in the hierarchical index |
| `verify_integrity` | bool, default `False` | If `True`, checks whether the new concatenated axis contains duplicates |
| `sort` | bool, default `False` | If `True`, it sorts the resulting Series or Dataframe by the keys |
| `copy` | bool, default `True` | If `False`, it avoids copying data unnecessarily |

## Example

mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved
The example below demonstrates the use of `.concat()` method:

```py
import pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
Expand All @@ -53,12 +55,12 @@ print(result)

```

The example will result in a new DataFrame is returned by concatenating df1 and df2 along the rows:
The example will result in a new DataFrame created by concatenating df1 and df2 along the rows. The output is as follows:

```shell
A B
0 1 2
1 3 4
0 5 6
1 7 8
0 1 3
1 2 4
0 5 7
1 6 8
```