Pandas Append() | Mastering in Python Pandas Library

Pandas Append() Function in Python

import pandas as pd
df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]})


df2 = pd.DataFrame({'A': [4,5,6],
                   'B': [40,50,60]})

display(df1 ,df2)
Output >>>
              A    B
          0   1   10
          1   2   20
          2   3   30
    
    
              A    B
          0   4   40
          1   5   50
          2   6   60

df1.append(df2)
Output >>>
              A    B
          0   1   10
          1   2   20
          2   3   30
          0   4   40
          1   5   50
          2   6   60
df1.append(df2, ignore_index = True)
Output >>>
              A    B
          0   1   10
          1   2   20
          2   3   30
          3   4   40
          4   5   50
          5   6   60
df2.append(df1, ignore_index = True)
Output >>>
              A    B
          0   4   40
          1   5   50
          2   6   60
          3   1   10
          4   2   20
          5   3   30
df1 = pd.DataFrame({'A': [1,2,3],
                   'B': [10,20,30]})


df2 = pd.DataFrame({'C': [4,5,6],
                   'B': [40,50,60]})

display(df1 ,df2)
Output >>>         
              A    B
          0   1   10
          1   2   20
          2   3   30
    
    
              C    B
          0   4   40
          1   5   50
          2   6   60
df1.append(df2, ignore_index = True)
Output >>>
          C:\Users\Shubham Matiyara\Anaconda3\lib\site- 
          packages\pandas\core\frame.py:6211: 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'.

            sort=sort)

                A    B     C
          0   1.0   10   NaN
          1   2.0   20   NaN
          2   3.0   30   NaN
          3   NaN   40   4.0
          4   NaN   50   5.0
          5   NaN   60   6.0
df1.append(df2, ignore_index = True, sort = False)
Output >>>
                A    B     C
          0   1.0   10   NaN
          1   2.0   20   NaN
          2   3.0   30   NaN
          3   NaN   40   4.0
          4   NaN   50   5.0
          5   NaN   60   6.0

Leave a Reply

Top