pandas
  1. pandas-concatenation

Concatenation - ( Pandas Reading and Writing Files )

Heading h2

Syntax

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

Example

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
df2 = pd.DataFrame({'A': [4, 5, 6], 'B': ['d', 'e', 'f']})

result = pd.concat([df1, df2])

print(result)

Output

   A  B
0  1  a
1  2  b
2  3  c
0  4  d
1  5  e
2  6  f

Explanation

Pandas provides the concat() function to concatenate two or more dataframes along a specific axis. It takes in a list of dataframes to be concatenated and returns a new concatenated dataframe.

In the above example, we create two dataframes df1 and df2 and then use the concat() function to concatenate them along the default axis 0 (axis=0). The result is a new dataframe result containing all the rows of both the dataframes.

Use

Concatenation is used in various data processing tasks where data needs to be combined or merged from different sources. It is particularly useful in combining tables with the same columns.

Important Points

  • Pandas provides the concat() function to concatenate two or more dataframes
  • The function takes in a list of dataframes to be concatenated and returns a new concatenated dataframe
  • The default axis for concatenation is 0 (rows), but can be changed to 1 (columns) using the axis parameter
  • The concat() function also has optional parameters such as join, keys, levels, etc.

Summary

In conclusion, the concat() function in Pandas is an essential tool for combining two or more dataframes. It provides flexibility in choosing the axis for concatenation, and optional parameters for customizing the concatenation process. Concatenation is a common data processing task, and Pandas simplifies this task with its powerful concat() function.

Published on: