How to set a custom separator in pandas to_csv()?

After all, I did:

df['Col'] = df['Col'].str.replace('|', ':')

In order to remove it from the column. Then I fixed a different character to separate my df.


Obviously Pandas seems not to allow this behavior.

However, if you absolutely want ":::". Why not exporting the dataframe with an uncommon character such as "|" and then open back the file and replace "|" by ":::".

That's the only solution I imagine to perform your desired result.


This is an old post, but I always seem to land here when googling how to export Dataframe to csv.

Although you can't do it directly with Pandas, you can do it with Numpy.

Since Pandas requires Numpy, you are not increasing your package size.

To do what you want, you can simply do:

import numpy as np
np.savetxt('out.csv', my_df, delimiter=':::')

Numpy offers a greater api to save csv files. You can even specify different separators using:

import numpy as np
np.savetxt('out.csv', my_df, fmt=['%.2f:::', '%f', '%s'])

You can find all the possible options in the docs.