Pandas convert a dataframe to a bed file?

1.3k Views Asked by At

I used pandas.to_csv() to convert a pandas dataframe to a BED file by doing this:

pd.to_csv('xxx.bed', index=False, sep='\t', header=None)

I want to know if this can successfully convert a dataframe to a bed file, or I am just exporting the dataframe as a csv file.

1

There are 1 best solutions below

0
mozway On

Bed files are following a fixed width format, so to_csv wouldn't be useful directly.

You have two options:

1- convert your columns to strings with padding to a fixed width, then export to csv

2- use tablulate to export as fixed width (see this answer for more details)