Search code examples
pythonpandasdataframedata-analysis

How do you stack two Pandas Dataframe columns on top of each other?


Is there a library function or correct way of stacking two Pandas data frame columns on top of each other?

For example make 4 columns into 2:

a1  b1  a2  b2
 1   2   3   4
 5   6   7   8

to

c   d
1   2
5   6
3   4
7   8

The documentation for Pandas Data Frames that I read for the most part only deal with concatenating rows and doing row manipulation, but I'm sure there has to be a way to do what I described and I am sure it's very simple.

Any help would be great.


Solution

  • You can select the first two and second two columns using pandas.DataFrame.iloc. Then, change the column name of both parts to c and d. Afterwards, you can just join them using pandas.concat.

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(np.arange(1, 9).reshape((2, 4)),
            columns=["a1", "b1", "a2", "b2"])
    
    part1 = df.iloc[:,0:2]
    part2 = df.iloc[:,2:4]
    
    new_columns = ["c", "d"]
    part1.columns = new_columns
    part2.columns = new_columns
    
    print pd.concat([part1, part2], ignore_index=True)
    

    This gives you:

       c  d
    0  1  2
    1  5  6
    2  3  4
    3  7  8