如何用python实现行列互换?

本人生物信息小白,最近想整理一批数据,需要excel里面有两万多列,由于excel实现不了,所以打算用软件或语言来解决,希望大神给与指导,在此感激不尽回复内容:
基本的python吧:

in [1]: a=[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
in [2]: print map(list,zip(*a))
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]

[[row[i] for row in matrix] for i in range(len(matrix[0]))]
用excel的话建议用pandas

import pandas as pd
df = pd.read_excel(‘你的文件路径’,’第几个sheet’, header = false) #读取文件 比如 df = pd.read_excel(‘c:/your_data.xlsx’,0, header = false)
df_t = df.t #获得矩阵的转置
df_t.to_excel(‘要保存的文件路径’, sheet_name=’我的表名’) #保存文件 比如 df_t.to_excel(‘c:/test.xlsx’, sheet_name=’sheet 1′)

可以用numpy里的matrix .t来转置
2w多列的话只能存成csv格式了。xlsx也就16384列

sample input:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Posted in 未分类

发表评论