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

59,201

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Try this

import pandas as pd
import numpy as np

my_numpy = pandas_df.to_numpy()
np.savetxt('out.csv', my_numpy,fmt='%s', delimiter=':::')

Solution 5

Zipa helped me with my problem of using consecutive spaces as seperator here:

This could be a workaround:

  myCsv = df.astype(str).apply(lambda x: '   '.join(x), axis=1)
  myCsv.rename('   '.join(df.columns)).to_csv(file, header=True, index=False)

Maybe based on his answer ,try :

myCsv = df.astype(str).apply(lambda x: ':::'.join(x), axis=1)
myCsv.rename(':::'.join(df.columns)).to_csv(file, header=True,index=False)

It did work for me, if te column names are strings

Share:
59,201

Related videos on Youtube

john doe
Author by

john doe

Updated on August 21, 2021

Comments

  • john doe
    john doe over 2 years

    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?.

  • john doe
    john doe about 7 years
    The problem is that I have | in my dataframe.
  • Jonathan DEKHTIAR
    Jonathan DEKHTIAR about 7 years
    Find one character that you don't have. Try "~" or "µ", maybe "¤".
  • Chris
    Chris about 7 years
    @johndoe, values containing | should be quoted, e.g.: foo|"bar|baz"
  • john doe
    john doe about 7 years
    Or how can I remove them from my df ?
  • john doe
    john doe about 7 years
    I tried to: df.replace('\b|\b', '-', regex = True)
  • Faheem Mitha
    Faheem Mitha over 2 years
    IMO, this answer is missing important details. See the answer by Kumar Abhisek.
  • Gustavo Lopes
    Gustavo Lopes over 2 years
    Which details are missing from this answer? I'm happy to improve it.

Related