python中如何将链表转换为矩阵

2024-03-29 07:13:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我的输入数据看起来像(输入文件)地址:

AGAP2     TCGA-BL-A0C8-01A-11R-A10U-07      66.7328
AGAP2     TCGA-BL-A13I-01A-11R-A13Y-07      186.8366
AGAP3     TCGA-BL-A13J-01A-11R-A10U-07      183.3767
AGAP3     TCGA-BL-A3JM-01A-12R-A21D-07      33.2927
AGAP3     TCGA-BT-A0S7-01A-11R-A10U-07      57.9040
AGAP3     TCGA-BT-A0YX-01A-11R-A10U-07      99.8540
AGAP4     TCGA-BT-A20J-01A-11R-A14Y-07      88.8278
AGAP4     TCGA-BT-A20N-01A-11R-A14Y-07      129.7021

我想要那个输出.txt看起来像:

        TCGA-BL-A0C8-01A-11R-A10U-07  TCGA-BL-A13I-01A-11R-A13Y-07  ...
AGAP2   66.7328                       186.8366
AGAP3   0                             0

Tags: 文件数据地址btbltcgaa14ya13j
1条回答
网友
1楼 · 发布于 2024-03-29 07:13:22

使用pandas:读取csv,创建pivot并写入csv。你知道吗

import pandas as pd

df = pd.read_table("input.txt", names="xy", sep=r'\s+')
# reset index first - we need named column
new = df.reset_index().pivot(index="index", columns='x', values='y')
new.fillna(0, inplace=True)
new.to_csv("output.csv", sep='\t')      # tab separated

result

Reshaping and Pivot Tables

编辑:填充空值

相关问题 更多 >