How to use pandas concat() to combine DataFrame/Series


Written By - Sravan Kumar

 

Advertisement

Pandas concat() function syntax

The pandas concat() function is used to join multiple pandas data structures along a specified axis and possibly perform union or intersection operations along other axes.

The following command explains the concat function:

concat(objs, axis=0, , join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False)

Parameters:

The elements of the concat function can be summarized as follows:

  • The objs function: A list or dictionary of Series, DataFrame, or Panel objects to be concatenated.
  • The axis function: The axis along which the concatenation should be performed. 0 is the default value.
  • The join function: The type of join to perform when handling indexes on other axes. The 'outer' function is the default.
  • The join_axes function: This is used to specify exact indexes for the remaining indexes instead of doing an outer/inner join.
  • The keys function: This specifies a list of keys to be used to construct a MultiIndex.

 

What are we going to cover?

We will discuss the following scenarios using pandas contact() function

  • Concatenate two Series with default parameters
  • Concatenate two Series horizontally
  • Concatenate two Series with ignore_index
  • Concatenate two Series with axis-0
  • Concatenate two DataFrames with default parameters
  • Concatenate two DataFrames horizontally
  • Concatenate two DataFrames with ignore_index
  • Concatenate two DataFrames with axis-0

 

Scenario-1: Concatenate two Series with default parameters

Here we are going to concat two series with no parameters

Example: In this example, we created two series and  concat two series with no parameters

# import pandas
import pandas 

# creating the Series-1
data1 = pandas.Series([10,20,30,50,60])

# creating the Series-2
data2 = pandas.Series(['Python','java','html','php','R'])

#display two series
print(data1,data2)

print()

# concate two series
print(pandas.concat([data1, data2]))

Output:

Advertisement
0    10
1    20
2    30
3    50
4    60
dtype: int64 0    Python
1      java
2      html
3       php
4         R
dtype: object

0        10
1        20
2        30
3        50
4        60
0    Python
1      java
2      html
3       php
4         R
dtype: object

 

Scenario-2: Concatenate two Series horizontally

Here we are going to concat two series horizontally by setting axis parameter to 1.

Example: In this example, we created two series and  concat two series horizontally by setting axis parameter to 1.

# import pandas
import pandas 

# creating the Series-1
data1 = pandas.Series([10,20,30,50,60])

# creating the Series-2
data2 = pandas.Series(['Python','java','html','php','R'])

#display two series
print(data1,data2)

print()

# concate two series
print(pandas.concat([data1, data2],axis=1))

Output:

0    10
1    20
2    30
3    50
4    60
dtype: int64 0    Python
1      java
2      html
3       php
4         R
dtype: object

    0       1
0  10  Python
1  20    java
2  30    html
3  50     php
4  60       R

 

Scenario-3: Concatenate two Series with ignore_index

Here we are going to concat two series horizontally by setting index parameter to True and False

Example 1: In this example, we are going to concat two series horizontally by setting index parameter to True

# import pandas
import pandas 

# creating the Series-1
data1 = pandas.Series([10,20,30,50,60])

# creating the Series-2
data2 = pandas.Series(['Python','java','html','php','R'])

#display two series
print(data1,data2)

print()

# concate two series
print(pandas.concat([data1, data2],ignore_index=True))

Output:

0    10
1    20
2    30
3    50
4    60
dtype: int64 0    Python
1      java
2      html
3       php
4         R
dtype: object

0        10
1        20
2        30
3        50
4        60
5    Python
6      java
7      html
8       php
9         R
dtype: object

 

Example 2: In this example, we are going to concat two series horizontally by setting index parameter to False

# import pandas
import pandas 

# creating the Series-1
data1 = pandas.Series([10,20,30,50,60])

# creating the Series-2
data2 = pandas.Series(['Python','java','html','php','R'])

#display two series
print(data1,data2)

print()

# concate two series
print(pandas.concat([data1, data2],ignore_index=False))

Output:

Advertisement
0    10
1    20
2    30
3    50
4    60
dtype: int64 0    Python
1      java
2      html
3       php
4         R
dtype: object

0        10
1        20
2        30
3        50
4        60
0    Python
1      java
2      html
3       php
4         R
dtype: object
dtype: object

 

Scenario-4: Concatenate two Series with axis-0

Here we are going to concat two series with axis = 0

Example: In this example, we are going to concat two series with axis = 0

# import pandas
import pandas 

# creating the Series-1
data1 = pandas.Series([10,20,30,50,60])

# creating the Series-2
data2 = pandas.Series(['Python','java','html','php','R'])

#display two series
print(data1,data2)

print()

# concate two series
print(pandas.concat([data1, data2],axis=0))

Output:

0    10
1    20
2    30
3    50
4    60
dtype: int64 0    Python
1      java
2      html
3       php
4         R
dtype: object

0        10
1        20
2        30
3        50
4        60
0    Python
1      java
2      html
3       php
4         R
dtype: object

 

Scenario-5: Concatenate two DataFrames with default parameters

Here we are going to concat two dataframes with no parameters

Example:  In this example, we are going to concat two dataframes with no parameters.

# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])

#display two DataFrames
print(data1)
print(data2)

print()

# concate two DataFrame
print(pandas.concat([data1, data2]))

Output:

Advertisement
0
0  10
1  20
2  30
3  50
4  60
        0
0  Python
1    java
2    html
3     php
4       R

        0
0      10
1      20
2      30
3      50
4      60
0  Python
1    java
2    html
3     php
4       R

 

Scenario-6: Concatenate two DataFrames horizontally

Here we are going to concat two dataframes horizontally by specifying axis = 1

Example: In this example, we are going to concat two dataframes horizontally by specifying axis = 1

# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])

#display two DataFrames
print(data1)
print(data2)

print()

# concate two DataFrame
print(pandas.concat([data1, data2],axis=1))

Output:

0
0  10
1  20
2  30
3  50
4  60
        0
0  Python
1    java
2    html
3     php
4       R

    0       0
0  10  Python
1  20    java
2  30    html
3  50     php
4  60       R

 

Scenario-7: Concatenate two DataFrames with ignore_index

Here we are going to concat two dataframes with ignore_index parameter

Example 1:  In this example, we are going to concat two dataframes with ignore_index parameter sets to True

# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])

#display two DataFrames
print(data1)
print(data2)

print()

# concate two DataFrame
print(pandas.concat([data1, data2],ignore_index=True))

Output:

0
0  10
1  20
2  30
3  50
4  60
        0
0  Python
1    java
2    html
3     php
4       R

        0
0      10
1      20
2      30
3      50
4      60
5  Python
6    java
7    html
8     php
9       R

 

Example 2:  In this example, we are going to concat two dataframes with ignore_index parameter sets to False

# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])

# display two DataFrames
print(data1)
print(data2)

print()

# concate two DataFrame
print(pandas.concat([data1, data2],ignore_index=False))

Output:

0
0  10
1  20
2  30
3  50
4  60
        0
0  Python
1    java
2    html
3     php
4       R

        0
0      10
1      20
2      30
3      50
4      60
0  Python
1    java
2    html
3     php
4       R

 

Scenario-8: Concatenate two DataFrames with axis-0

Here we are going to concat two dataframes with axis-0

Example: In this example, we are going to concat two dataframes with axis-0

Advertisement
# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])

#display two DataFrames
print(data1)
print(data2)

print()

# concate two DataFrame
print(pandas.concat([data1, data2],axis=0))

Output:

0
0  10
1  20
2  30
3  50
4  60
        0
0  Python
1    java
2    html
3     php
4       R

        0
0      10
1      20
2      30
3      50
4      60
0  Python
1    java
2    html
3     php
4       R

 

Scenario-9: Perform inner and outer join with concat() function

Example: In this example, we are performing  inner and outer join on the two dataframes.

# import pandas
import pandas 

# creating the dataframe -1
data1 = pandas.DataFrame([10,20,30,50,60])

# creating the dataframe-2
data2 = pandas.DataFrame(['Python','java','html','php','R'])


# concate two DataFrame
print(pandas.concat([data1, data2],join='outer',axis=1))

# concate two DataFrame
print(pandas.concat([data1, data2],join='inner',axis=1))

Output:

    0       0
0  10  Python
1  20    java
2  30    html
3  50     php
4  60       R
    0       0
0  10  Python
1  20    java
2  30    html
3  50     php
4  60       R

 

Summary

In this tutorial we discussed how to concat Series and DataFrame in pandas using concat() function, By considering all the parameters we  concluded this tutorial.

 

References

Pandas - concat()

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment