QBoard » Big Data » Big Data - Others » Concatenate dataframes Pandas

Concatenate dataframes Pandas

  • I have three dataframes. Their shapes are (2656, 246), (2656, 2412) and (2656, 7025). I want to merge dataframes as above:

    enter image description here

    So It will result a (2656, 9683) Dataframe. Thanks for any help.

    Typo on image: on Dataframe 3, it will 7025, not 5668.

      September 15, 2021 3:02 PM IST
    0
  • Let’s understand how we can concatenate two or more Data Frames. A concatenation of two or more data frames can be done using pandas.concat()  method. concat() in pandas works by combining Data Frames across rows or columns. We can concat two or more data frames either along rows  (axis=0) or along columns (axis=1)

    import pandas as pd
    import numpy as np
    
    
    df1 = pd.DataFrame(np.random.randint(25, size=(4, 4)),
    				index=["1", "2", "3", "4"],
    				columns=["A", "B", "C", "D"])
    
    df2 = pd.DataFrame(np.random.randint(25, size=(6, 4)),
    				index=["5", "6", "7", "8", "9", "10"],
    				columns=["A", "B", "C", "D"])
    
    df3 = pd.DataFrame(np.random.randint(25, size=(4, 4)),
    				columns=["A", "B", "C", "D"])
    
    df4 = pd.DataFrame(np.random.randint(25, size=(4, 4)),
    				columns=["E", "F", "G", "H"])
    
    display(df1, df2, df3, df4)
    
      September 21, 2021 12:47 PM IST
    0
  • Assuming that the rows are in same order that you wish to merge all of the dataframes, you can use the concat command specificying axis=1.
    new_df = pd.concat([df1, df2, df3], axis=1)
    If the row index for each of the data frames are different and you want to merge them in the current order, you can also apply ignore_index:
    new_df = pd.concat([df1, df2, df3], ignore_index=True)
    For details on the merge, join & concatenation operations, please refer to the pandas docs.
      September 15, 2021 11:13 PM IST
    0