简体   繁体   English

如何将 pandas dataframe 转换为每列不同格式的 CSV 格式?

[英]How can I get a pandas dataframe into CSV format with different formats per columns?

Issues for pandas' DataFrame text output functions:熊猫 DataFrame 文本 output 函数的问题:

  • to_csv() does not support the 'formatters' parameter of to_string(). to_csv() 不支持 to_string() 的“格式化程序”参数。 I need different formats for each column.我需要为每一列使用不同的格式。
  • to_string() does not support a separator. to_string() 不支持分隔符。

What I do so far:到目前为止我做了什么:

I generate a list of formatting strings in the new Python formatting format, so eg ['{8.1f}','{9.3f}',...,] , and then do this hack:我以新的 Python 格式化格式生成格式化字符串列表,例如['{8.1f}','{9.3f}',...,] ,然后执行以下操作:

f.write(', '.join(fmt).format(*data)+'\r\n')

Is there a way I can have pandas do some of this hacking for me or is that a feature request and I already did all the work?有没有办法让 pandas 为我做一些黑客攻击,或者这是一个功能请求,我已经完成了所有工作? ;) ;)

np.savetxt can do it (by supplying fmt argument, which can be a list ), but that means the column names has to be written separately ( header argument): np.savetxt可以做到(通过提供fmt参数,它可以是一个list ),但这意味着必须单独编写列名( header参数):

np.savetxt('temp.csv', df.values, fmt=['%8.1f','%9.3f','%8.1f','%9.3f','%8.1f'], 
           delimiter=',', header='     '+'     ,'.join(df.columns), comments='')

You can try this:你可以试试这个:

import pandas as pd

df = pd.DataFrame({
    'a': [-8.114445, 888.303217, 80],
    'b': ['d', 'gg5g', '9ghhhh'],
    'c': [14.34e+4,12.1e-1,1e-5],
    'd': [34,65373,-176576]}
)

df.to_string("temp.txt",
            formatters={
                "a": "{:6,.2f}".format,
                "b": "{:<}".format,
                "c": "{:9,.5e}".format,
                "d": "{:7d}".format
                }, index=False
            )

Content of "temp.txt": “temp.txt”的内容:

     a      b           c       d
 -8.11      d 1.43400e+05      34
888.30   gg5g 1.21000e+00   65373
 80.00 9ghhhh 1.00000e-05 -176576

暂无
暂无

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

相关问题 When converting a pandas dataframe to csv, how can i seperate the headers of the dataframe into different columns of the csv-file? - When converting a pandas dataframe to csv, how can i seperate the headers of the dataframe into different columns of the csv-file? 如何通过python格式化我的pandas数据框,以在csv输出中获取列? - How do I format my pandas dataframe through python to get columns in the csv output? 使用 Pandas DataFrame 到 CSV 时,如何在每列上指定不同的十进制格式? - How can I specify a different decimal format on each column when using Pandas DataFrame to CSV? 我们如何将不同的格式应用于 dataframe 中的不同列? - How can we apply different formats to different columns in a dataframe? 如何将文件格式读取为不同的文件格式? 例如 - SAV 作为 CSV - How can i read in file formats as a different file format? e.g. - SAV as CSV 将pandas DataFrame写入Excel,使用不同列的不同格式 - Writing pandas DataFrame to Excel with different formats for different columns 如何获取pandas中不同列的日期格式 - How to get the date format of different Columns in pandas 如何优雅地在指定列上的 pandas dataframe 中获得 corr? - How can I get corr in pandas dataframe on specified columns elegantly? 如何将熊猫数据帧附加到具有不同列的_csv - How to append pandas dataframe to_csv with different columns 如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式? - How to convert two different date formats from a pandas dataframe column into same format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM