如何在 Pandas 中融合两个数据框
How to melt two data frames in Pandas
我有两个数据框:
In [14]: rep1
Out[14]:
x y z
A 1 2 3
B 4 5 6
C 1 1 2
In [15]: rep2
Out[15]:
x y z
A 7 3 4
B 3 3 3
使用此代码创建:
import pandas as pd
rep1 = pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6]),('C',[1,1,2])], orient='index', columns=['x', 'y', 'z'])
rep2 = pd.DataFrame.from_items([('A', [7, 3, 4]), ('B', [3, 3, 3])], orient='index', columns=['x', 'y', 'z'])
然后我想做的是对 rep1
和 rep2
进行网格划分,这样结果如下:
gene rep1 rep2 type
A 1 7 x
B 4 3 x
A 2 3 y
B 5 3 y
A 3 4 z
B 6 3 z
C 行被跳过,因为它不被 rep1
和 rep2
共享。
我怎样才能做到这一点?
>>> c1 = rep1.values.T.flatten()
>>> c2 = rep2.values.T.flatten()
>>> c3 = np.vstack((rep1.columns.values, rep2.columns.values)).T.flatten()
>>> pd.DataFrame(np.vstack((c1,c2,c3)).T)
0 1 2
0 1 7 x
1 4 3 x
2 2 3 y
3 5 3 y
4 3 4 z
5 6 3 z
编辑:当我回答这个问题时,问题根本没有C行。现在事情更复杂了,但我还是把它留在这里。
这样做:
df =pd.concat([rep1.stack(),rep2.stack()],axis=1).reset_index().dropna()
df.columns =['GENE','TYPE','REP1','REP2']
df.sort(columns=['TYPE','GENE'], inplace=True)
连接 axis =1
上的堆叠数据帧。重置索引可以让您返回基因和类型列。 dropna
处理为基因 c 产生的空值。添加正确的列名等
returns:
GENE TYPE REP1 REP2
0 A x 1 7
3 B x 4 3
1 A y 2 3
4 B y 5 3
2 A z 3 4
5 B z 6 3
我有两个数据框:
In [14]: rep1
Out[14]:
x y z
A 1 2 3
B 4 5 6
C 1 1 2
In [15]: rep2
Out[15]:
x y z
A 7 3 4
B 3 3 3
使用此代码创建:
import pandas as pd
rep1 = pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6]),('C',[1,1,2])], orient='index', columns=['x', 'y', 'z'])
rep2 = pd.DataFrame.from_items([('A', [7, 3, 4]), ('B', [3, 3, 3])], orient='index', columns=['x', 'y', 'z'])
然后我想做的是对 rep1
和 rep2
进行网格划分,这样结果如下:
gene rep1 rep2 type
A 1 7 x
B 4 3 x
A 2 3 y
B 5 3 y
A 3 4 z
B 6 3 z
C 行被跳过,因为它不被 rep1
和 rep2
共享。
我怎样才能做到这一点?
>>> c1 = rep1.values.T.flatten()
>>> c2 = rep2.values.T.flatten()
>>> c3 = np.vstack((rep1.columns.values, rep2.columns.values)).T.flatten()
>>> pd.DataFrame(np.vstack((c1,c2,c3)).T)
0 1 2
0 1 7 x
1 4 3 x
2 2 3 y
3 5 3 y
4 3 4 z
5 6 3 z
编辑:当我回答这个问题时,问题根本没有C行。现在事情更复杂了,但我还是把它留在这里。
这样做:
df =pd.concat([rep1.stack(),rep2.stack()],axis=1).reset_index().dropna()
df.columns =['GENE','TYPE','REP1','REP2']
df.sort(columns=['TYPE','GENE'], inplace=True)
连接 axis =1
上的堆叠数据帧。重置索引可以让您返回基因和类型列。 dropna
处理为基因 c 产生的空值。添加正确的列名等
returns:
GENE TYPE REP1 REP2
0 A x 1 7
3 B x 4 3
1 A y 2 3
4 B y 5 3
2 A z 3 4
5 B z 6 3