Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
datascience
communitycreator

How to concatenate or link pandas objects along a given axis

Alek Akim

Overview

We make use of the concat() function to concatenate, join or link pandas objects (DataFrame or series) along a specified axis.

Syntax

The concat() function takes the syntax given below:

pandas.concat(objs, axis=0, join='outer', ignore_index=False, levels=None, names=None, verify_integrity=False, copy=True)
Syntax for the concat() function in Pandas

Parameter

The concat() function takes the following parameter values:

  • obj (required): This represents a sequence or mapping of Dataframe or series objects.
  • axis (optional): This represents the axis along which the concatenation is done.
  • join (optional): This is used in handling the indexes on other axis.
  • ignore_index(optional): This takes a Boolean value indicating if the index values along the concatenation axis is used (if False) or not be used (if True).
  • levels (optional): This represents the specific levels which are used for constructing a multiple index.
  • names (optional): This represents the names for the levels.
  • verify_integrity (optional): This is used to test if the new linked axis contains duplicate or not.
  • copy (optional): This takes a Boolean value indicating if the data is copied or not.

Return value

import pandas as pd

# creating a dataframe
a = pd.Series(['A', 'B', 'C', 'D', 'E'])
b = pd.Series(['F', 'G', 'H', 'I', 'J'])

# concatenating the series
d = pd.concat([a,b], ignore_index="True")

# printing the concatenated series
print(d)
Implementing the concat() function in Pandas

Explanation

Here's an explanation of the above code:

  • Line 1: We import the pandas library.
  • Line 4–5: We create a sequence of series objects, a and b using the series() function.
  • Line 8: We concatenate a and b using the concat() function. The result is assigned to a variable, d.
  • Line 11: We print the concatenated series, d.

RELATED TAGS

pandas
datascience
communitycreator
RELATED COURSES

View all Courses

Keep Exploring