【发布时间】:2015-02-04 16:42:05
【问题描述】:
我有以下代码:
import pandas as pd
rep1 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux']), ('RP1',[1.00,23.22,11.12]),('RP1',["A","B","C"]) ], orient='columns')
rep2 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux']), ('RP2',[3.33,77.22,18.12]),('RP2',["G","I","K"]) ], orient='columns')
rep3 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'k']), ('Gene', ['foo', 'bar', 'kux']), ('RP3',[99.99,98.29,8.10]),('RP2',["M","P","J"]) ], orient='columns')
tmp = []
tmp.append(rep1)
tmp.append(rep2)
tmp.append(rep3)
这会产生以下数据框列表。
In [56]: tmp
Out[56]:
[ Probe Gene RP1 RP1
0 x foo 1.00 A
1 y bar 23.22 B
2 z qux 11.12 C, Probe Gene RP2 RP2
0 x foo 3.33 G
1 y bar 77.22 I
2 z qux 18.12 K, Probe Gene RP3 RP2
0 x foo 99.99 M
1 y bar 98.29 P
2 k kux 8.10 J]
上面的每个数据框都有以下特点:
- 始终包含 4 列
- 第二列和最后一列具有相同的名称
- 前两列始终命名为
Probe和Gene -
Probe和Gene的内容总是一致地出现,即。 'x' 总是与 'foo' 一起使用。
我正在尝试合并列表中的那些 DF,以便它生成:
Probe Gene RP1 RP2 RP3 RP1 RP2 RP3
0 x foo 1.00 3.33 99.99 A G M
1 y bar 23.22 77.22 98.29 B I P
2 z qux 11.12 18.12 NA C K NA
3 k kux NA NA 8.10 NA NA J
我试过这段代码但失败了:
In [67]: reduce(pd.merge,tmp)
MergeError: Left data columns not unique: Index([u'Probe', u'Gene', u'RP1', u'RP1'], dtype='object')
正确的做法是什么?
【问题讨论】:
-
你应该参考这个问题/答案stackoverflow.com/q/28292426/1240268