简体   繁体   English

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

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

From the docs I know that in order to save as a .csv file 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.我也试过: df.to_csv('../data.csv', sep='\\s*\\:::', index=False) ,得到了相同的结果。 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.这是一篇旧帖子,但在谷歌搜索如何将 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. Numpy 提供了一个更大的 api 来保存 csv 文件。 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 .您可以在文档中找到所有可能的选项。

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.这是我想象中执行您想要的结果的唯一解决方案。

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。

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)

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如果列名是字符串,它确实对我有用

Pandas version 0.24.2.熊猫版本 0.24.2。 Putting comment by Jonathan Dekhtiar as an answer.将 Jonathan Dekhtiar 的评论作为答案。 Saving the csv with special characters like α works like a charm用像 α 这样的特殊字符保存 csv 就像一个魅力

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=':::')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM