使用python处理生物信息数据(六)
2020-03-08 本文已影响0人
你猜我菜不菜
Python学习的第六天,主要学习处理表格数据。
1. 表格中列的融合
table = [
['protein', 'ext1', 'ext2', 'ext3'],
[0.16, 0.038, 0.044, 0.040],
[0.33, 0.089, 0.095, 0.091],
[0.66, 0.184, 0.191, 0.191],
[1.00, 0.280, 0.292, 0.283],
[1.32, 0.365, 0.367, 0.365],
[1.66, 0.441, 0.443, 0.444]
]
table
Out[112]:
[['protein', 'ext1', 'ext2', 'ext3'],
[0.16, 0.038, 0.044, 0.04],
[0.33, 0.089, 0.095, 0.091],
[0.66, 0.184, 0.191, 0.191],
[1.0, 0.28, 0.292, 0.283],
[1.32, 0.365, 0.367, 0.365],
[1.66, 0.441, 0.443, 0.444]]
table = table[1:]
table
Out[114]:
[[0.16, 0.038, 0.044, 0.04],
[0.33, 0.089, 0.095, 0.091],
[0.66, 0.184, 0.191, 0.191],
[1.0, 0.28, 0.292, 0.283],
[1.32, 0.365, 0.367, 0.365],
[1.66, 0.441, 0.443, 0.444]]
protein, ext1, ext2, ext3 = zip(*table)
print(protein,ext1,ext2,ext3)
(0.16, 0.33, 0.66, 1.0, 1.32, 1.66) (0.038, 0.089, 0.184, 0.28, 0.365, 0.441) (0.044, 0.095, 0.191, 0.292, 0.367, 0.443) (0.04, 0.091, 0.191, 0.283, 0.365, 0.444)
extinction = ext1 + ext2 + ext3
print(extinction)
(0.038, 0.089, 0.184, 0.28, 0.365, 0.441, 0.044, 0.095, 0.191, 0.292, 0.367, 0.443, 0.04, 0.091, 0.191, 0.283, 0.365, 0.444)
protein = protein * 3
print(protein)
(0.16, 0.33, 0.66, 1.0, 1.32, 1.66, 0.16, 0.33, 0.66, 1.0, 1.32, 1.66, 0.16, 0.33, 0.66, 1.0, 1.32, 1.66)
table = zip(protein, extinction)
for prot, ext in table:
print(prot, ext)
0.16 0.038
0.33 0.089
0.66 0.184
1.0 0.28
1.32 0.365
1.66 0.441
0.16 0.044
0.33 0.095
0.66 0.191
1.0 0.292
1.32 0.367
1.66 0.443
0.16 0.04
0.33 0.091
0.66 0.191
1.0 0.283
1.32 0.365
1.66 0.444
2. ZIP()函数的使用
data = [
[0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23]
]
columns = list(zip(*data))
print(columns)
[(0, 10, 20), (1, 11, 21), (2, 12, 22), (3, 13, 23)]
3. 创建一个表格
table = []
for i in range(6):
table.append([0] * 6)
print(table)
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
4. 用字典展示表格
table = [
{'protein': 0.16, 'ext1': 0.038, 'ext2': 0.044, 'ext3': 0.040},
{'protein': 0.33, 'ext1': 0.089, 'ext2': 0.095, 'ext3': 0.091},
{'protein': 0.66, 'ext1': 0.184, 'ext2': 0.191, 'ext3': 0.191},
{'protein': 1.00, 'ext1': 0.280, 'ext2': 0.292, 'ext3': 0.283},
{'protein': 1.32, 'ext1': 0.365, 'ext2': 0.367, 'ext3': 0.365},
{'protein': 1.66, 'ext1': 0.441, 'ext2': 0.443, 'ext3': 0.444}
]
cell = table[1]['ext2']
print(table)
[{'protein': 0.16, 'ext1': 0.038, 'ext2': 0.044, 'ext3': 0.04}, {'protein': 0.33, 'ext1': 0.089, 'ext2': 0.095, 'ext3': 0.091}, {'protein': 0.66, 'ext1': 0.184, 'ext2': 0.191, 'ext3': 0.191}, {'protein': 1.0, 'ext1': 0.28, 'ext2': 0.292, 'ext3': 0.283}, {'protein': 1.32, 'ext1': 0.365, 'ext2': 0.367, 'ext3': 0.365}, {'protein': 1.66, 'ext1': 0.441, 'ext2': 0.443, 'ext3': 0.444}]
print(cell)
0.095
5. 从文件中读取表格
table = []
for line in open('lowry_data.txt','r'):
table.append(line.strip().split('\t'))
print(table)
5. 将表格存入文件
table = [
['protein', 'ext1', 'ext2', 'ext3'],
[0.16, 0.038, 0.044, 0.040],
[0.33, 0.089, 0.095, 0.091],
[0.66, 0.184, 0.191, 0.191],
[1.00, 0.280, 0.292, 0.283],
[1.32, 0.365, 0.367, 0.365],
[1.66, 0.441, 0.443, 0.444]
]
out = ''
for row in table:
line = [str(cell) for cell in row]
out = out + '\t'.join(line) + '\n'
# print(type(out))
# print(type(line))
open('lowry_data2.txt', 'w').write(out)
Out[13]: 158