2015-10-07 86 views
1

我想用python(python 3.4)合併表中的數據。 我的示例數據如下所示,我想獲得這種結果表。Python - 合併兩個表中的數據

[表1]

Name1 Name2 
AAAA XXXX 
BBBB YYYY 
CCCC ZZZZ 

[表2]

Index1 Sample1 Sample2 Sample3 
AAAA 10 20 30 
BBBB 25 25 25 
CCCC 30 31 32 
XXXX 27 29 31 
YYYY 45 21 56 
ZZZZ 48 24 10 

[結果表]

Index2 Sample1 Sample2 Sample3 
AAAA+XXXX 37 49 61 
BBBB+YYYY 70 46 81 
CCCC+ZZZZ 78 55 42 

雖然似乎是一個簡單的問題,我找不到好的解決方案因爲我是一個Python新手,我不熟悉python庫。如果我在數據庫上使用SQL,可能很容易,但我想在沒有數據庫的情況下解決它。 有沒有人有好主意?

+3

我建議先將「表2」讀入關係數據類型,如Python字典。由此你有你的關鍵價值對。然後,您可以解析「表1」文件以查看要將哪些值添加在一起。 – enpenax

+1

看看[Pandas](http://pandas.pydata.org/)。特別是[DataFrame加入和合並]一節(http://pandas.pydata.org/pandas-docs/stable/merging.html#database-style-dataframe-joining-merging)。 – Evert

+0

表格中的數據如何存儲?在'.txt'文件中? – ZdaR

回答

2

以下csv方法會爲你的樣本數據的工作:

import csv 

with open('table2.txt', 'r') as f_table2: 
    csv_table2 = csv.reader(f_table2, delimiter=' ', skipinitialspace=True) 
    table2_header = next(csv_table2) 
    table2_data = {cols[0] : cols[1:] for cols in csv_table2} 

with open('table1.txt', 'r') as f_table1, open('output.csv', 'w', newline='\n') as f_output: 
    csv_table1 = csv.reader(f_table1, delimiter=' ', skipinitialspace=True) 
    table1_header = next(csv_table1) 
    csv_output = csv.writer(f_output) 
    csv_output.writerow(table2_header) 

    csv_output.writerows(
     ['{}+{}'.format(cols[0], cols[1])] + [int(x) + int(y) for x, y in zip(table2_data[cols[0]], table2_data[cols[1]])] for cols in csv_table1) 

這會給你一個輸出CSV文件如下:

Index1,Sample1,Sample2,Sample3 
AAAA+XXXX,37,49,61 
BBBB+YYYY,70,46,81 
CCCC+ZZZZ,78,55,42 

使用Python 3.4.3進行測試

+0

csv模塊似乎對於處理txt文件也非常有用。你的代碼適合我的樣本。我想用更大的數據集來測試csv方法和熊貓方法。感謝您的幫助。 – ToBeSpecific

1

如果使用純Python的工作(不包括第三方庫,如numpy的),這將有可能做這種方式:

class Entry: 
    def __init__(self, index, sample1, sample2, sample3): 
     self.index = index 
     self.sample1 = sample1 
     self.sample2 = sample2 
     self.sample3 = sample3 

    def __add__(self, other): 
     return '{index2} {sample1} {sample2} {sample3}'.format(
      index2=self.index + '+' + other.index, 
      sample1=self.sample1 + other.sample1, 
      sample2=self.sample2 + other.sample2, 
      sample3=self.sample3 + other.sample3, 
     ) 


def read_table(path_to_data): 
    def extract_body(content): 
     return [e.strip().split(' ') for e in content[1:]] 

    with open(path_to_data, 'r') as f: 
     content = f.readlines() 
    return extract_body(content) 


content1 = read_table('data1.txt') 
content2 = read_table('data2.txt') 

entries = [Entry(e[0], int(e[1]), int(e[2]), int(e[3])) for e in content2] 

# output 
print('Index2 Sample1 Sample2 Sample3') 

for line in content1: 
    entry1 = next(e for e in entries if e.index == line[0]) 
    entry2 = next(e for e in entries if e.index == line[1]) 

    print(entry1 + entry2) 
+1

由於我是python的新手,我想盡可能地使用現成的庫。然而,我很驚訝你使用沒有數據處理庫的純Python,並且學會了一些關於如何處理這類工作的知識。感謝您的幫助。 – ToBeSpecific

+0

@ToBeSpecific https:// xkcd。com/353/ – ewilazarus

+0

我想我必須學習更多與python飛行。 – ToBeSpecific