Pandas Concat() | Mastering in Python Pandas Library

Pandas concate() Function in Python

import pandas as pd
sr1 = pd.Series([0,1,2])
sr1
Output >>>
          0    0
          1    1
          2    2
          dtype: int64
sr2 = pd.Series([3,4,5,6,7])
sr2
Output >>>
          0    3
          1    4
          2    5
          3    6
          4    7
          dtype: int64
pd.concat([sr1, sr2])
Output >>>
          0    0
          1    1
          2    2
          0    3
          1    4
          2    5
          3    6
          4    7
          dtype: int64
df1 = pd.DataFrame({'ID': [1,2,3,4],
                  'Name': ['A', 'B', 'C','D'],
                  'Class': [5,6,7,8]})
df1
Output >>>
             ID  Name  Class
          0   1     A      5
          1   2     B      6
          2   3     C      7
          3   4     D      8
df2 = pd.DataFrame({'ID': [5,6,7,8],
                  'Name': ['E', 'F', 'G', 'H'],
                  'Class': [9,10,11,12]})
df2
Output >>>
             ID  Name  Class
          0   5     E      9
          1   6     F     10
          2   7     G     11
          3   8     H     12
pd.concat([df2, df1])
Output >>>
             ID  Name  Class
          0   5     E      9
          1   6     F     10
          2   7     G     11
          3   8     H     12
          0   1     A      5
          1   2     B      6
          2   3     C      7
          3   4     D      8
pd.concat([df2, df1], axis = 1)
Output >>>
          
            ID  Name  Class  ID  Name  Class
         0   5     E      9   1     A      5
         1   6     F     10   2     B      6
         2   7     G     11   3     C      7
         3   8     H     12   4     D      8
pd.concat([df1, df2], axis = 0, ignore_index = True)
Output >>>
          
            ID  Name  Class
         0   1     A      5
         1   2     B      6
         2   3     C      7
         3   4     D      8
         4   5     E      9
         5   6     F     10
         6   7     G     11
         7   8     H     12
df1 = pd.DataFrame({'ID': [1,2,3,4],
                  'Name': ['A', 'B', 'C','D'],
                  'Class': [5,6,7,8]})
df1
Output >>>
          	ID	Name	Class
         0	 1	   A	    5
         1	 2	   B	    6
         2	 3	   C	    7
         3	 4	   D	    8
df2 = pd.DataFrame({'ID': [3,4],
                  'Name': ['C','D'],
                  'Class': [7,8]})
df2
Output >>>        
            ID	Name  Class
         0   3	   C      7
         1   4	   D	  8
pd.concat([df1, df2])
Output >>>
          	ID	Name  Class
          0	 1	   A	  5
          1	 2	   B	  6
          2	 3	   C	  7
          3	 4	   D	  8
          0	 3	   C	  7
          1	 4	   D	  8
pd.concat([df1, df2], axis = 1)
Output >>>
          	ID	Name  Class	  ID	Name  Class
          0	 1	   A	  5	 3.0	   C	7.0
          1	 2	   B	  6	 4.0	   D	8.0
          2	 3	   C	  7	 NaN	 NaN	NaN
          3	 4	   D	  8	 NaN	 NaN	NaN
pd.concat([df1, df2], axis = 1, join = 'inner')
Output >>>
               ID  Name  Class	 ID  Name  Class
          0	1     A	     5	  3	C      7
          1	2     B	     6	  4     D      8
pd.concat([df1, df2], axis = 1, join_axes = [df1.index])
Output >>>
               ID  Name	 Class	  ID	 Name	Class
          0	1     A	     5	 3.0	    C	  7.0
          1	2     B	     6	 4.0	    D	  8.0
          2	3     C	     7	 NaN	  NaN	  NaN
          3	4     D	     8	 NaN	  NaN	  NaN

pd.concat([df1, df2], axis = 1, join_axes = [df2.index])
Output >>>
             ID  Name  Class	ID  Name  Class
          0   1     A	   5	 3     C      7
          1   2     B	   6	 4     D      8
df1 = pd.DataFrame({'ID': [1,2,3,4],
                  'Name': ['A', 'B', 'C','D'],
                  'Class': [5,6,7,8]})
df1
Output >>>
          	ID	Name	Class
          0	 1	   A	    5
          1	 2	   B	    6
          2	 3	   C	    7
          3	 4	   D	    8
df2 = pd.DataFrame({'ID': [5,6,7,8],
                  'Name': ['E', 'F', 'G', 'H'],
                  'Class': [9,10,11,12]})
df2
Output >>>
             ID  Name  Class
         0    5	    E	   9
         1    6	    F	  10
         2    7	    G	  11
         3    8	    H     12
pd.concat([df1, df2], keys = ['df1','df2'])
Output >>>
                 ID	Name	Class
              0	  1	   A	    5
          df1 1	  2	   B	    6
              2	  3	   C	    7
              3	  4	   D	    8
              0	  5	   E	    9
          df2 1	  6	   F	   10
              2	  7	   G	   11
              3	  8	   H	   12
pd.concat([df1, df2], keys = ['First df','Second df'])
Output >>>
                         ID	Name	Class
                      0	  1	   A	    5
          First df    1	  2	   B	    6
                      2	  3	   C	    7
                      3	  4	   D	    8
               	      0	  5	   E	    9
          Second df   1	  6	   F	   10
                      2	  7	   G	   11
                      3	  8	   H	   12

pd.concat([df1, df2], axis = 1,  keys = ['First df','Second df'])
Output >>>
                          First df	            Second df
               ID	Name	Class	ID	Name	Class
          0     1	   A	    5	 5	   E	    9
          1     2	   B	    6	 6	   F	   10
          2     3	   C	    7	 7	   G	   11
          3     4	   D	    8	 8	   H	   12

df1 = pd.DataFrame({'ID': [1,2,3,4],
                  'Name': ['A', 'B', 'C','D'],
                  'Class': [5,6,7,8]})
df1
Output >>>
          	ID	Name	Class
          0	 1	   A	    5
          1	 2	   B	    6
          2	 3	   C	    7
          3	 4	   D	    8
df2 = pd.DataFrame({'Marks': [40, 63, 91, 34]})
df2
Output >>>
          	Marks
         0	   40
         1	   63
         2	   91
         3	   34
pd.concat([df1, df2])
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  """Entry point for launching an IPython kernel.


Output >>>
            ID	Name	Class	Marks
         0	1.0	   A	  5.0	  NaN
         1	2.0	   B	  6.0	  NaN
         2	3.0	   C	  7.0	  NaN
         3	4.0	   D	  8.0	  NaN
         0	NaN	 NaN	  NaN	  40.0
         1	NaN	 NaN	  NaN	  63.0
         2	NaN	 NaN	  NaN	  91.0
         3	NaN	 NaN	  NaN	  34.0
pd.concat([df1, df2], sort = False)
Output >>>
            ID	Name	Class	Marks
         0	1.0	   A	  5.0	  NaN
         1	2.0	   B	  6.0	  NaN
         2	3.0	   C	  7.0	  NaN
         3	4.0	   D	  8.0	  NaN
         0	NaN	 NaN	  NaN	  40.0
         1	NaN	 NaN	  NaN	  63.0
         2	NaN	 NaN	  NaN	  91.0
         3	NaN	 NaN	  NaN	  34.0

Download Jupyter file of Pandas Concat Function source code

Visit the official site of pandas.concat

Leave a Reply

Top