Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 961
alacy Avatar asked Jan 24 '26 04:01

alacy


2 Answers

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
like image 92
Carsten Avatar answered Jan 25 '26 18:01

Carsten


I would do the following

import pandas as pd
df = pd.DataFrame({'a1' : pd.Series([1,5]), 'b1' : pd.Series([2,6]), 'a2' : pd.Series([3,7]), 'b2' : pd.Series([4,8])})

df1 = df[['a1','b1']]
df2 = df[['a2','b2']]
df1.columns = ['c','d']
df2.columns = ['c','d']
df1.append(df2)

I just saw that @Carsten answered this question as well and I agree with his answer too

like image 32
Evan Volgas Avatar answered Jan 25 '26 17:01

Evan Volgas