Home >Backend Development >Python Tutorial >如何用python实现行列互换?

如何用python实现行列互换?

WBOY
WBOYOriginal
2016-06-06 16:24:285825browse

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

回复内容:

基本的python吧:
<code class="language-text">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]]
</code>
[[row[i] for row in matrix] for i in range(len(matrix[0]))] 用excel的话建议用pandas
<code class="language-text">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')
</code>
可以用numpy里的matrix .T来转置 2W多列的话只能存成CSV格式了。
xlsx也就16384列

sample input:

<code class="language-text">1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn