Home > Software design >  Transpose adds another row/column to dataframe
Transpose adds another row/column to dataframe

Time:09-29

When trying to transpose the following table:

Sample input_test input_test2 input_test3 ip_test ip_test2 ip_test3
tRNA 5 5 5 1 1 1
CDS 9330 9330 9330 26680 26680 26680
3utr 2525 2525 2525 3810 3810 3810
5utr 1966 1966 1966 5006 5006 5006
5ss 960 960 960 1972 1972 1972
3ss 932 932 932 1989 1989 1989
proxintron 1221 1221 1221 1228 1228 1228
distintron 6450 6450 6450 4744 4744 4744
noncoding_exon 1477 1477 1477 1302 1302 1302
noncoding_5ss 25 25 25 35 35 35
noncoding_3ss 16 16 16 29 29 29
noncoding_proxintron 68 68 68 80 80 80
noncoding_distintron 629 629 629 441 441 441
allexonic 15298 15298 15298 36798 36798 36798
allintronic 10301 10301 10301 10518 10518 10518
all 27131 27131 27131 49060 49060 49060

I get this:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sample tRNA CDS 3utr 5utr 5ss 3ss proxintron distintron noncoding_exon noncoding_5ss noncoding_3ss noncoding_proxintron noncoding_distintron allexonic allintronic all
input_test 5 9330 2525 1966 960 932 1221 6450 1477 25 16 68 629 15298 10301 27131
input_test2 5 9330 2525 1966 960 932 1221 6450 1477 25 16 68 629 15298 10301 27131
input_test3 5 9330 2525 1966 960 932 1221 6450 1477 25 16 68 629 15298 10301 27131
ip_test 1 26680 3810 5006 1972 1989 1228 4744 1302 35 29 80 441 36798 10518 49060
ip_test2 1 26680 3810 5006 1972 1989 1228 4744 1302 35 29 80 441 36798 10518 49060
ip_test3 1 26680 3810 5006 1972 1989 1228 4744 1302 35 29 80 441 36798 10518 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.

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")

CodePudding user response:

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.

CodePudding user response:

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

  • Related