gpt4 book ai didi

python - 在 pandas 中格式化包含两行的表格

转载 作者:行者123 更新时间:2023-12-03 07:55:16 24 4
gpt4 key购买 nike

您好,我有一个数据框,例如

Start End Feature Qualifier Function
1 35 CDS Product Gene1
36 67 CDS Product Putative_actin
69 123 CDS Product 1_hypothetical protein
345 562 CDS Product 2_hypothetical protein

我想将此表格式化为:

第 1 行:

  • 第 1 列:开始
  • 第 2 列:结束
  • 第 3 栏:功能

第二行:

  • 第 4 栏:限定符
  • 第 5 栏:功能

并且每个由“特征Seq_number_of_the_row”分隔

在这个例子的最后,我应该得到:

>Feature Seq1
1 35 CDS
Product Gene1
>Feature Seq2
36 67 CDS
Product Putative_actin
>Feature Seq3
69 123 CDS
Product 1_hypothetical protein
>Feature Seq4
345 562 CDS
Product 2_hypothetical protein

每列应该用制表符分隔。

最佳答案

带表格:

for idx, (_, row) in enumerate(df.iterrows(), 1):
print(f'>Feature Seq{idx}')
print(f'\t{row.Start}\t{row.End}\t{row.Feature}')
print(f'\t\t\t\t{row.Qualifier}\t{row.Function}')

# Output
>Feature Seq1
1 35 CDS
Product Gene1
>Feature Seq2
36 67 CDS
Product Putative_actin
>Feature Seq3
69 123 CDS
Product 1_hypothetical protein
>Feature Seq4
345 562 CDS
Product 2_hypothetical protein

您可以使用:

for idx, (_, row) in enumerate(df.iterrows(), 1):
print(f'>Feature Seq{idx}')
print(f'{row.Start:<5}{row.End:<5}{row.Feature}')
print(f'{"":15}{row.Qualifier:<15}{row.Function}')

# Output

>Feature Seq1
1 35 CDS
Product Gene1
>Feature Seq2
36 67 CDS
Product Putative_actin
>Feature Seq3
69 123 CDS
Product 1_hypothetical protein
>Feature Seq4
345 562 CDS
Product 2_hypothetical protein

关于python - 在 pandas 中格式化包含两行的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76219039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com