[英]Python - merging data in two tables
我想將表中的數據與python(python 3.4)合並。 我的示例數據如下所示,我希望得到那種結果表。
[表格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庫不熟悉。 如果我在DB上使用SQL可能很容易,但我想在沒有DB的情況下解決它。 有沒有人有好主意?
以下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進行測試
如果您正在使用純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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.