pd.to_csv详解

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

[python]  view plain  copy
  1. import os  
  2. os.getcwd() #获取当前工作路径  
2.to_csv()是DataFrame类的方法,read_csv()是pandas的方法

转自:https://blog.csdn.net/toshibahuai/article/details/79034829

[python]  view plain  copy
  1. dt.to_csv() #默认dt是DataFrame的一个实例,参数解释如下  

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

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

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

  • 格式 float_format: Format string for floating point numbers
[python]  view plain  copy
  1. dt.to_csv('C:/Users/think/Desktop/Result1.csv',float_format='%.2f'#保留两位小数  

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

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

  • 是否保留行索引 index:  whether to write row (index) names (default True)
[python]  view plain  copy
  1. dt.to_csv('C:/Users/think/Desktop/Result1.csv',index=0#不保存行索引  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值