From the docs I know that in order to save as a .csv
file one can simply do:
df.to_csv(sep = ';')
However, I would like to use my custom separator, for instance: :::
. How can I set :::
as a separator?. I tried to:
df.to_csv(sep = ':::')
And got: TypeError: "delimiter" must be a 1-character string
Also I tried to: df.to_csv('../data.csv', sep='\s*\:::', index=False)
, and got the same result. Thus, How can I set my own separator?.
UPDATE
Since I have in my dataframe |
, I can not use such character as a separator. I tried to removed it with:
df.replace('\b|\b', '-', regex = True)
However, it did not worked. Any alternative on how to remove it?.
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:
Numpy offers a greater api to save csv files. You can even specify different separators using:
You can find all the possible options in the docs.
After all, I did:
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.
Pandas version 0.24.2. Putting comment by Jonathan Dekhtiar as an answer. Saving the csv with special characters like α works like a charm
Zipa helped me with my problem of using consecutive spaces as seperator here:
Maybe based on his answer ,try :
It did work for me, if te column names are strings