简体   繁体   English

转置将另一行/列添加到 dataframe

[英]Transpose adds another row/column to dataframe

When trying to transpose the following table:尝试转置下表时:

Sample样本 input_test输入测试 input_test2输入_test2 input_test3输入_test3 ip_test ip_test ip_test2 ip_test2 ip_test3 ip_test3
tRNA转录核糖核酸 5 5个 5 5个 5 5个 1 1个 1 1个 1 1个
CDS光盘 9330 9330 9330 9330 9330 9330 26680 26680 26680 26680 26680 26680
3utr 3utr 2525 2525 2525 2525 2525 2525 3810 3810 3810 3810 3810 3810
5utr 5utr 1966 1966年 1966 1966年 1966 1966年 5006 5006 5006 5006 5006 5006
5ss 5ss 960 960 960 960 960 960 1972 1972年 1972 1972年 1972 1972年
3ss 3ss 932 932 932 932 932 932 1989 1989 1989 1989 1989 1989
proxintron proxinron 1221 1221 1221 1221 1221 1221 1228 1228 1228 1228 1228 1228
distintron分化子 6450 6450 6450 6450 6450 6450 4744 4744 4744 4744 4744 4744
noncoding_exon非编码外显子 1477 1477 1477 1477 1477 1477 1302 1302 1302 1302 1302 1302
noncoding_5ss非编码_5ss 25 25 25 25 25 25 35 35 35 35 35 35
noncoding_3ss非编码_3ss 16 16 16 16 16 16 29 29 29 29 29 29
noncoding_proxintron非编码_proxintron 68 68 68 68 68 68 80 80 80 80 80 80
noncoding_distintron noncoding_distintron 629 629 629 629 629 629 441 441 441 441 441 441
allexonic变态反应 15298 15298 15298 15298 15298 15298 36798 36798 36798 36798 36798 36798
allintronic全电子 10301 10301 10301 10301 10301 10301 10518 10518 10518 10518 10518 10518
all全部 27131 27131 27131 27131 27131 27131 49060 49060 49060 49060 49060 49060

I get this:我明白了:

0 0 1 1个 2 2个 3 3个 4 4个 5 5个 6 6个 7 7 8 8个 9 9 10 10 11 11 12 12 13 13 14 14 15 15
Sample样本 tRNA转录核糖核酸 CDS光盘 3utr 3utr 5utr 5utr 5ss 5ss 3ss 3ss proxintron proxinron distintron分化子 noncoding_exon非编码外显子 noncoding_5ss非编码_5ss noncoding_3ss非编码_3ss noncoding_proxintron非编码_proxintron noncoding_distintron noncoding_distintron allexonic变态反应 allintronic全电子 all全部
input_test输入测试 5 5个 9330 9330 2525 2525 1966 1966年 960 960 932 932 1221 1221 6450 6450 1477 1477 25 25 16 16 68 68 629 629 15298 15298 10301 10301 27131 27131
input_test2输入_test2 5 5个 9330 9330 2525 2525 1966 1966年 960 960 932 932 1221 1221 6450 6450 1477 1477 25 25 16 16 68 68 629 629 15298 15298 10301 10301 27131 27131
input_test3输入_test3 5 5个 9330 9330 2525 2525 1966 1966年 960 960 932 932 1221 1221 6450 6450 1477 1477 25 25 16 16 68 68 629 629 15298 15298 10301 10301 27131 27131
ip_test ip_test 1 1个 26680 26680 3810 3810 5006 5006 1972 1972年 1989 1989 1228 1228 4744 4744 1302 1302 35 35 29 29 80 80 441 441 36798 36798 10518 10518 49060 49060
ip_test2 ip_test2 1 1个 26680 26680 3810 3810 5006 5006 1972 1972年 1989 1989 1228 1228 4744 4744 1302 1302 35 35 29 29 80 80 441 441 36798 36798 10518 10518 49060 49060
ip_test3 ip_test3 1 1个 26680 26680 3810 3810 5006 5006 1972 1972年 1989 1989 1228 1228 4744 4744 1302 1302 35 35 29 29 80 80 441 441 36798 36798 10518 10518 49060 49060

Why is there an additional row with numbers on top?为什么上面多了一行数字? This is driving me nuts, I've tried so many options with index=False , df_merged.set_index('Sample',inplace=True) , etc.这让我抓狂,我已经尝试了很多index=Falsedf_merged.set_index('Sample',inplace=True)等选项。

This is the code:这是代码:

# transpose

    df_merged = pd.read_csv(str(save_path) + "all_stats_matrix.csv")

    df_merged = df_merged.T

    df_merged.to_csv(str(save_path) + "all_stats_matrix.csv")

You could alternatively if your goal is to have the first row as headers instead of the numbers.如果您的目标是将第一行作为标题而不是数字,您也可以选择。 Use this:用这个:

df_merged = pd.read_csv(str(save_path) + "all_stats_matrix.csv")

df_merged = df_merged.T

df_merged = df_merged.rename(columns=df_merged.iloc[0]).drop(df_merged.index[0])

df_merged.to_csv(str(save_path) + "all_stats_matrix.csv")

It is not the most clean way, but should get the job done.这不是最干净的方式,但应该完成工作。

How about: df_merged.set_index('Sample').transpose() ?怎么样: df_merged.set_index('Sample').transpose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM