Python 如何在 Pandas to_csv() 中设置自定义分隔符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43324461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:52:06  来源:igfitidea点击:

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

pythonpython-3.xcsvpandas

提问by john doe

From the docs I know that in order to save as a .csvfile one can simply do:

从文档中我知道为了保存为.csv文件,可以简单地执行以下操作:

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

并得到: 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?.

我也尝试:df.to_csv('../data.csv', sep='\s*\:::', index=False),并得到相同的结果。因此,如何设置我自己的分隔符?。

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)

df.replace('\b|\b', '-', regex = True)

However, it did not worked. Any alternative on how to remove it?.

然而,它没有奏效。关于如何删除它的任何替代方法?

采纳答案by Jonathan DEKHTIAR

Obviously Pandas seems not to allow this behavior.

显然 Pandas 似乎不允许这种行为。

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.

这是我想象中执行您想要的结果的唯一解决方案。

回答by Gustavo Lopes

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

这是一篇旧帖子,但在谷歌搜索如何将 Dataframe 导出到 csv 时,我似乎总是登陆这里。

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

虽然你不能直接用 Pandas 来做,但你可以用 Numpy 来做。

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

由于 Pandas 需要 Numpy,因此您不会增加包大小。

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:

Numpy 提供了一个更大的 api 来保存 csv 文件。您甚至可以使用以下方法指定不同的分隔符:

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

You can find all the possible options in the docs.

您可以在文档中找到所有可能的选项。

回答by john doe

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.

以便将其从列中移除。然后我修复了一个不同的字符来分隔我的 df。

回答by k0ngcrete

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

Zipa 帮助我解决了在此处使用连续空格作为分隔符的问题

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)

这可能是一种解决方法:

  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

如果列名是字符串,它确实对我有用