Reputation: 5074
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.
Upvotes: 3
Views: 17117
Reputation: 557
Alternatively, using melt:
# Make data as in previous answers
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(1, 9).reshape((2, 4)),
columns=["a1", "b1", "a2", "b2"])
# Melt both columns and concatenate
df = pd.concat([
df[['a1', 'a2']].melt(value_name='c'),
df[['b1', 'b2']].melt(value_name='d')],
axis=1)
# Discard unwanted columns melt creates
df = df[['c', 'd']]
Upvotes: 0
Reputation: 2921
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
Upvotes: 1
Reputation: 18446
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
Upvotes: 7