Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按预定义列连接/合并数据帧列表_Python_Pandas - Fatal编程技术网

Python 按预定义列连接/合并数据帧列表

Python 按预定义列连接/合并数据帧列表,python,pandas,Python,Pandas,我有以下数据帧列表: import pandas as pd rep1 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux']), ('RP1',[1.00,23.22,11.12])], orient='columns') rep2 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'b

我有以下数据帧列表:

import pandas as pd
rep1 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux']), ('RP1',[1.00,23.22,11.12])], orient='columns')
rep2 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux']), ('RP2',[11.33,31.25,22.12])], orient='columns')
rep3 = pd.DataFrame.from_items([('Probe', ['x', 'y', 'z']), ('Gene', ['foo', 'bar', 'qux'])], orient='columns')
tmp = []
tmp.append(rep1)
tmp.append(rep2)
tmp.append(rep3)

# In actuality the DF could be more than 3.
产生:

In [53]: tmp
Out[53]:
[  Probe Gene    RP1
 0     x  foo   1.00
 1     y  bar  23.22
 2     z  qux  11.12,   Probe Gene    RP2
 0     x  foo  11.33
 1     y  bar  31.25
 2     z  qux  22.12,   Probe Gene
 0     x  foo
 1     y  bar
 2     z  qux]
我想做的是连接该数据帧列表,以便它产生以下结果:

  Probe Gene      RP1        RP2
0     x  foo     1.00      11.33
1     y  bar    23.22      31.25
2     z  qux    11.12      22.12
请注意,
rep3
仅包含两列。在连接过程中,我们希望自动放弃它

我尝试了这个代码,但没有用。正确的方法是什么

In [57]: full_df = pd.concat(tmp,axis=1).fillna(0)

In [58]: full_df
Out[58]:
  Probe Gene    RP1 Probe Gene    RP2 Probe Gene
0     x  foo   1.00     x  foo  11.33     x  foo
1     y  bar  23.22     y  bar  31.25     y  bar
2     z  qux  11.12     z  qux  22.12     z  qux
我不确定这是正确的方法,但一种简洁的方法是:


这基本上相当于:

tmp[0].merge(tmp[1]).merge(tmp[2])...

注意:这意味着如果您在tmp中有很多数据帧,那么它可能不如使用concat那么有效。

连接意味着将它们放在一起并保留所有内容。你所描述的听起来更像是加入。你读过加入吗?@BrenBarn:Merge以两个数据帧作为输入。但我的问题是输入是数据帧列表(2个或更多)。您可能会发现这很有用。您可以将探针和基因列设置为索引,然后使用
concat
,如图所示。
tmp[0].merge(tmp[1]).merge(tmp[2])...