【问题标题】:Merging more than two files with one column and common index in pandas在熊猫中将两个以上的文件与一列和公共索引合并
【发布时间】:2013-09-25 08:04:08
【问题描述】:

我有 10 个包含两列的 .csv 文件。例如

file1.csv

Bact1,[1821932:1822487](+)
Bact2,[555760:556294](+)
Bact3,[2901866:2902424](-)
Bact4,[1104980:1105544](+)

file2.csv

Bact1,[1973928:1975194](-)
Bact2,[972152:973499](+)
Bact3,[3001035:3002739](-)
Bact4,[3331158:3332481](+)
Bact5,[712517:713771](+)
Bact5,[1376120:1377386](-)

file3.csv

Bact6,[4045708:4047781](+)

以此类推 file10.csv Bact1 代表细菌种类,包括符号在内的所有数字代表基因的位置。每个文件代表一个不同的基因,并且像file2.csv一样存在重复

我想合并这些文件,这样我就有了这样的东西

Bact1    [1821932:1822487](+)    [1973928:1975194](-)    NaN
Bact2    [555760:556294](+)      [972152:973499](+)      NaN
Bact3    [2901866:2902424](-)    [3001035:3002739](-)    NaN
Bact4    [1104980:1105544](+)    [3331158:3332481](+)    NaN
Bact5    NaN                     [712517:713771](+)      NaN     
Bact5    NaN                        [1376120:1377386](-)    NaN
Bact6    NaN                     NaN                     [4045708:4047781](+)

我曾尝试在 python 中使用 pandas 包,但似乎大多数函数都旨在合并两个数据帧,不超过两个,或者我遗漏了一些东西。

我上周刚开始用 python 编程(我通常使用 R),所以陷入可能或至少是一件简单的事情。

我现在正在使用:

    for x in range(1,10):
        df[x]=pandas.read_csv("file%s.csv" % (x),header=None,index_col=[0])
        df[x].columns=['gene%s' % (x)]
   dfjoin={}
   dfjoin=df[1].join([df[2],df[3],df[4],df[5],df[6],df[7],df[8],df[9],df[10]])

结果:

0                          gene1           gene2             gene3                 
Starkeya-novella-DSM-506     NaN     [728886:730173](+)  [731445:732615](+)     
Starkeya-novella-DSM-506     NaN     [728886:730173](+)  [9662:10994](+)    
Starkeya-novella-DSM-506     NaN     [728886:730173](+)  [9662:10994](+)     
Starkeya-novella-DSM-506     NaN     [728886:730173](+)  [9662:10994](+) 

查看gene2和gene3,它已经复制了重复的结果。

【问题讨论】:

  • 如果file1中有两个Bact5行,file2中有三个Bact5行,你想要输出多少个Bact5行?如果是六个,那么你可以使用@Andy Hayden 的 join 方法。

标签: python csv pandas


【解决方案1】:

假设您已将这些作为 DataFrames 阅读如下:

In [11]: df1 = pd.read_csv('file1.csv', sep=',', header=None, index_col=[0], names=['bact', 'file1'])

In [12]: df1
Out[12]: 
                      file1
bact
Bact1  [1821932:1822487](+)
Bact2    [555760:556294](+)
Bact3  [2901866:2902424](-)
Bact4  [1104980:1105544](+)

那么你可以简单地join他们:

In [21]: df1.join([df2, df3])
Out[21]: 
                      file1                 file2                 file3
bact
Bact1  [1821932:1822487](+)  [1973928:1975194](-)                   NaN
Bact2    [555760:556294](+)    [972152:973499](+)                   NaN
Bact3  [2901866:2902424](-)  [3001035:3002739](-)                   NaN
Bact4  [1104980:1105544](+)  [3331158:3332481](+)                   NaN
Bact5                   NaN    [712517:713771](+)                   NaN
Bact5                   NaN  [1376120:1377386](-)                   NaN
Bact6                   NaN                   NaN  [4045708:4047781](+)

【讨论】:

  • 嗨@Andy 我再次在我的问题中添加了代码。该代码似乎有效,但有很多重复。我想知道怎么做。
  • @microbetic 当您使用 2x2 重复项进行连接时,您会在连接的 DataFrame 中获得 4 个。在这种情况下,尚不清楚 pandas 应该如何加入,因此您需要对其更加明确(并告诉它您想要什么)。
  • 嗨@Andy,我想要“外部”或基本上是索引的联合。但我似乎得到重复的索引不是跟随 NaN,而是跟随重复的值。使用上面的代码和'outer'查看结果的sn-p:
  • 在类似的说明中,有没有一种方法可以基于索引合并值。例如,不将 Bact5 列在两行中,我们可以将其与 file2 对应的值合并到一行中,并用分隔符分隔吗?
【解决方案2】:

我稍微修改了你的示例数据,代码如下:

import pandas as pd
import io

data = {
"file1":"""Bact1,[1821932:1822487](+)
Bact2,[555760:556294](+)
Bact3,[2901866:2902424](-)
Bact4,[1104980:1105544](+)
Bact5,[1104981:1105544](+)
Bact5,[1104982:1105544](+)""",

"file2":"""Bact1,[1973928:1975194](-)
Bact2,[972152:973499](+)
Bact3,[3001035:3002739](-)
Bact4,[3331158:3332481](+)
Bact5,[712517:713771](+)
Bact5,[1376120:1377386](-)
Bact5,[1376121:1377386](-)""",

"file3":"""Bact4,[3331150:3332481](+)
Bact6,[4045708:4047781](+)"""}

def read_file(f):
    s = pd.read_csv(f, header=None, index_col=0, squeeze=True)
    return s.groupby(s.index).apply(lambda s:pd.Series(s.values))

series = {key:read_file(io.StringIO(unicode(text)))
          for key, text in data.items()}

print pd.concat(series, axis=1)

输出:

                        file1                 file2                 file3
0                                                                        
Bact1 0  [1821932:1822487](+)  [1973928:1975194](-)                   NaN
Bact2 0    [555760:556294](+)    [972152:973499](+)                   NaN
Bact3 0  [2901866:2902424](-)  [3001035:3002739](-)                   NaN
Bact4 0  [1104980:1105544](+)  [3331158:3332481](+)  [3331150:3332481](+)
Bact5 0  [1104981:1105544](+)    [712517:713771](+)                   NaN
      1  [1104982:1105544](+)  [1376120:1377386](-)                   NaN
      2                   NaN  [1376121:1377386](-)                   NaN
Bact6 0                   NaN                   NaN  [4045708:4047781](+)

【讨论】:

  • 嗨@HYRY我有10个这样的文件,我不明白你是如何形成“数据”的?你是怎么读到这些文件的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2016-08-01
  • 2016-05-16
  • 2020-08-06
  • 2018-02-03
  • 1970-01-01
相关资源
最近更新 更多