pandas to_csv places extra lines in print out

  csv, dataframe, pandas, python

I am using pandas to filter a file and then write out some information. But for some reason it places an extra new line (n) in between the lines i want. Its far to big to edit manually it has over 500,000 lines.

I have a data frame:

                  CHROM      POS REF ALT  ...  4_3       4_5       4_6       4_9
0       NC_037638.1    56585   T   A  ...  1.0  1.000000  1.000000  1.000000
1       NC_037638.1    56596   G   A  ...  0.0  0.000000  0.000000  0.000000
2       NC_037638.1    59559   G   A  ...  1.0  1.000000  1.000000  1.000000
3       NC_037638.1    60391   C   T  ...  0.0  0.000000  0.000000  0.000000
4       NC_037638.1    60423   A   G  ...  1.0  1.000000  0.734694  0.166667

I then print out the first two columns into a text file. I would like them separated by a tab like so:

NC_037638.1    56585
NC_037638.1    56596
NC_037638.1    59559
NC_037638.1    60391
NC_037638.1    60423
NC_037638.1    60488
NC_037638.1    60604
NC_037638.1    60606
NC_037638.1    60610
NC_037638.1    60634

so I use this code, where the out_file is my text file from the function:

df.to_csv(out_file, sep='t', index = False)

What I get is this:

NC_037638.1   56585

NC_037638.1   56596

NC_037638.1   59559

NC_037638.1   60391

NC_037638.1   60423

NC_037638.1   60488

NC_037638.1   60604

NC_037638.1   60606

The subsequent program I use will not read this because it has an extra new line in between every part of information.

I’m sure this is something basic, but its hard to find out what is happening.

thanks.

Source: Python Questions

LEAVE A COMMENT