Skip to content

Commit b68f778

Browse files
[Term Entry] Python:Pandas built-in-functions: .concat() (#4814)
* Create concat.md * Lint + Format * Created concat.md file * Added information up to the Syntax * Finished concat.md * Update yarn.lock updated the yarn.lock file * Update term-entry-template.md updated the term-entry file * Edited line 19 * Applied changes and suggestions * Revised Syntax section * Update concat.md * Added Codebyte Example * Update concat.md minor changes * Update content/pandas/concepts/built-in-functions/terms/concat/concat.md * Update content/pandas/concepts/built-in-functions/terms/concat/concat.md * Catalog Content Changes * Update content/pandas/concepts/built-in-functions/terms/concat/concat.md * Update content/pandas/concepts/built-in-functions/terms/concat/concat.md * Update content/pandas/concepts/built-in-functions/terms/concat/concat.md ---------
1 parent 2167055 commit b68f778

File tree

1 file changed

+74
-0
lines changed
  • content/pandas/concepts/built-in-functions/terms/concat

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
Title: '.concat()'
3+
Description: 'Concatenates multiple dataframes or series along a particular axis.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data Structures'
9+
- 'Functions'
10+
- 'Pandas'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
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.
17+
18+
## Syntax
19+
20+
```pseudo
21+
pandas.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
22+
```
23+
24+
- `objs`: Denotes the objects to concatenate, which can be a sequence or mapping of pandas `Series` or `DataFrame` objects. It must be specified and can be passed as a list, tuple, dictionary, Series, or DataFrame.
25+
- `axis`: Specifies the axis to concatenate the objects. The default value is 0 for rows, while 1 represents columns.
26+
- `join`: Determines how to handle indexes on other axes. Options include "outer" (default), "inner," "left," or "right."
27+
- `ignore_index`: If `True`, reset the index in the resulting DataFrame. The new axis will be labeled 0, ..., n-1. The default value is `False`.
28+
- `keys`: Constructs a hierarchical index using the provided keys as the outermost level. The default value is `None`.
29+
- `levels`: Specific levels to use for constructing a MultiIndex if keys are provided. The default value is `None`.
30+
- `names`: Names for the levels generated in the hierarchical index. The default value is `None`.
31+
- `verify_integrity`: If `True`, checks whether the new concatenated axis contains duplicates. The default value is `False`.
32+
- `sort`: If `True`, sorts the resulting `Series` or `Dataframe` by the keys. The default value is `False`.
33+
- `copy`: If `False`, avoid copying data unnecessarily. The default value is `True`.
34+
35+
> **Note:** Only the `objs` parameter is required; all other parameters are optional.
36+
37+
## Example
38+
39+
The example below demonstrates the use of `.concat()` method:
40+
41+
```py
42+
import pandas as pd
43+
44+
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
45+
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
46+
47+
result = pd.concat([df1, df2])
48+
print(result)
49+
```
50+
51+
The example will result in a new `DataFrame` created by concatenating `df1` and `df2` along the rows. The output is as follows:
52+
53+
```shell
54+
A B
55+
0 1 3
56+
1 2 4
57+
0 5 7
58+
1 6 8
59+
```
60+
61+
## Codebyte Example
62+
63+
The code demonstrates the `.concat()` function on two DataFrames, concatenating `df1` and `df2` column-wise (`axis=1`) and using `keys` to create a hierarchical column index:
64+
65+
```codebyte/python
66+
import pandas as pd
67+
68+
df1 = pd.DataFrame({'A' : [1,2,3,4,5], 'B' : [6,7,8,9,10]})
69+
df2 = pd.DataFrame({'C' : [11,12,13,14,15], 'D' : [16,17,18,19,20]})
70+
71+
result = pd.concat([df1, df2], axis=1, keys = ['df1', 'df2'])
72+
73+
print(result)
74+
```

0 commit comments

Comments
 (0)