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

久未见 提交于 2019-12-08 00:49:45

问题


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.


回答1:


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



回答2:


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




回答3:


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']]


来源:https://stackoverflow.com/questions/27513890/how-do-you-stack-two-pandas-dataframe-columns-on-top-of-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!