我有以下代码:
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]
上述每个数据框都具有以下特征:
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')
这样做的正确方法是什么?
答案 0 :(得分:1)
您可以重复删除列名称。这是一种hacky方式:
In [11]: list(rep1.columns[0:2]) + [rep1.columns[2] + "_value"] + [rep1.columns[2] + "_letter"]
Out[11]: ['Probe', 'Gene', 'RP1_value', 'RP1_letter']
In [12]: for rep in tmp:
.....: rep.columns = list(rep.columns[0:2]) + [rep.columns[2] + "_value"] + [rep.columns[2] + "_letter"]
In [13]: reduce(pd.merge,tmp)
Out[13]:
Probe Gene RP1_value RP1_letter RP2_value RP2_letter RP3_value RP3_letter
0 x foo 1.00 A 3.33 G 99.99 M
1 y bar 23.22 B 77.22 I 98.29 P
您还需要将其指定为外部合并(以获取NaN行):
In [21]: reduce(lambda x, y: pd.merge(x, y, how='outer'),tmp)
Out[21]:
Probe Gene RP1_value RP1_letter RP2_value RP2_letter RP3_value RP3_letter
0 x foo 1.00 A 3.33 G 99.99 M
1 y bar 23.22 B 77.22 I 98.29 P
2 z qux 11.12 C 18.12 K NaN NaN
3 k kux NaN NaN NaN NaN 8.10 J