简体   繁体   English

合并和拆分熊猫数据框中的列

[英]Merge and split columns in pandas dataframe

I want to know how to merge multiple columns, and split them again. 我想知道如何合并多个列,然后再次拆分。

Input data 输入数据

A B C
1 3 5
2 4 6

Merge A, B, C to one column X 将A,B,C合并到一栏X

X
1
2
3
4
5
6

Process something with X, then split X into A, B, C again. 用X处理某物,然后再次将X分为A,B,C。 The number of rows for A, B, C is same(2). A,B,C的行数相同(2)。

A B C
1 3 5
2 4 6

Is there any simple way for this work? 有什么简单的方法可以完成这项工作吗?

Start with df: 从df开始:

   A  B  C
0  1  3  5
1  2  4  6

Next, get all values in one column: 接下来,在一列中获取所有值:

df2 = df.unstack().reset_index(drop=True).rename('X').to_frame()

print(df2)

   X
0  1
1  2
2  3
3  4
4  5
5  6

And, convert back to original shape: 并且,转换回原始形状:

df3  = pd.DataFrame(df2.values.reshape(2,-1, order='F'), columns=list('ABC'))
print(df3)

   A  B  C
0  1  3  5
1  2  4  6

Setup 设定

df=pd.DataFrame({'A': {0: 1, 1: 2}, 'B': {0: 3, 1: 4}, 'C': {0: 5, 1: 6}})

df
Out[684]: 
   A  B  C
0  1  3  5
1  2  4  6

Solution

Merge df to 1 column: 将df合并到1列:

df2 = pd.DataFrame(df.values.flatten('F'),columns=['X'])
Out[686]: 
   X
0  1
1  2
2  3
3  4
4  5
5  6

Split it back to 3 columns: 将其拆分为3列:

pd.DataFrame(df2.values.reshape(-1,3,order='F'),columns=['A','B','C'])
Out[701]: 
   A  B  C
0  1  3  5
1  2  4  6

un unwind in the way you'd like, you need to either unstack or ravel with order='F' 按照您想要的方式展开,您需要使用order='F'进行unstackravel

Option 1 选项1

def proc1(df):
    v = df.values
    s = v.ravel('F')
    s = s * 2
    return pd.DataFrame(s.reshape(v.shape, order='F'), df.index, df.columns)

proc1(df)

   A  B   C
0  2  6  10
1  4  8  12

Option 2 选项2

def proc2(df):
    return df.unstack().mul(2).unstack(0)


proc2(df)

   A  B   C
0  2  6  10
1  4  8  12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM