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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM