zoukankan      html  css  js  c++  java
  • pandas的to_csv()使用方法

    1.首先查询当前的工作路径:

    import os
    os.getcwd() #获取当前工作路径
    2.to_csv()是DataFrame类的方法,read_csv()是pandas的方法
    dt.to_csv() #默认dt是DataFrame的一个实例,参数解释如下

    路径 path_or_buf: A string path to the file to write or a StringIO
    dt.to_csv('Result.csv') #相对位置,保存在getwcd()获得的路径下
    dt.to_csv('C:/Users/think/Desktop/Result.csv') #绝对位置

    分隔符 sep : Field delimiter for the output file (default ”,”)
    dt.to_csv('C:/Users/think/Desktop/Result.csv',sep='?')#使用?分隔需要保存的数据,如果不写,默认是,

    替换空值 na_rep: A string representation of a missing value (default ‘’)
    dt.to_csv('C:/Users/think/Desktop/Result1.csv',na_rep='NA') #确实值保存为NA,如果不写,默认是空

    格式 float_format: Format string for floating point numbers
    dt.to_csv('C:/Users/think/Desktop/Result1.csv',float_format='%.2f') #保留两位小数

    是否保留某列数据 cols: Columns to write (default None)
    dt.to_csv('C:/Users/think/Desktop/Result.csv',columns=['name']) #保存索引列和name列

    是否保留列名 header: Whether to write out the column names (default True)
    dt.to_csv('C:/Users/think/Desktop/Result.csv',header=0) #不保存列名

    是否保留行索引 index:  whether to write row (index) names (default True)
    dt.to_csv('C:/Users/think/Desktop/Result1.csv',index=0) #不保存行索引
    ---------------------
    作者:暖心生
    来源:CSDN
    原文:https://blog.csdn.net/toshibahuai/article/details/79034829
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    BAT脚本批量调用Sql执行文件 (SqlServer 数据库)
    树莓派系统刻录到首次登陆等问题
    数据库视图的使用
    MQ配置安装
    PLSQL集合类型
    PLSQL-包函数存储过程
    Oracle 字段拆分替换在合并成一条
    ORACLE-EXP和IMP方法介绍
    javascript几个月前的时间
    返回顶部实现方式
  • 原文地址:https://www.cnblogs.com/telwanggs/p/10402299.html
Copyright © 2011-2022 走看看