数据加载、存储于文件格式:将数据写出到文本格式
import sys import pandas as pd data = pd.read_csv("examples/ex5.csv") print(data) ''' something a b c d message 0 one 1 2 3.0 4 NaN 1 two 5 6 NaN 8 world 2 three 9 10 11.0 12 foo ''' data.to_csv("ex_out/out.csv") # 逗号分隔 ''' ,something,a,b,c,d,message 0,one,1,2,3.0,4, 1,two,5,6,,8,world 2,three,9,10,11.0,12,foo ''' data.to_csv(sys.stdout,sep='|') ''' |something|a|b|c|d|message 0|one|1|2|3.0|4| 1|two|5|6||8|world 2|three|9|10|11.0|12|foo ''' # 缺失值在输出时会表示为空字符串,也可以表示为别的标记值 data.to_csv(sys.stdout,na_rep='NULL') ''' ,something,a,b,c,d,message 0,one,1,2,3.0,4,NULL 1,two,5,6,NULL,8,world 2,three,9,10,11.0,12,foo ''' # 可禁用行列标签 data.to_csv(sys.stdout,index=False,header=False) ''' one,1,2,3.0,4, two,5,6,,8,world three,9,10,11.0,12,foo ''' # 只写一部分列,并以指定顺序排列 data.to_csv(sys.stdout,index=False,columns=['a','b','c']) ''' a,b,c 1,2,3.0 5,6, 9,10,11.0 ''' data.to_csv(sys.stdout,index=False,columns=['a','c','b']) ''' a,c,b 1,3.0,2 5,,6 9,11.0,10 '''
import pandas as pd import numpy as np from pandas import Series datas = pd.date_range('1/1/2021',periods=7) print(datas) ''' DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06', '2021-01-07'], dtype='datetime64[ns]', freq='D') ''' ts = Series(np.arange(7),index=datas) ts.to_csv("ex_out/tseries.csv") ''' 2021-01-01,0 2021-01-02,1 2021-01-03,2 2021-01-04,3 2021-01-05,4 2021-01-06,5 2021-01-07,6 ''' data = Series.from_csv("ex_out/tseries.csv",parse_dates=True) print(data) ''' 2021-01-01 0 2021-01-02 1 2021-01-03 2 2021-01-04 3 2021-01-05 4 2021-01-06 5 2021-01-07 6 dtype: int64 '''
本文来自博客园,作者:OTAKU_nicole,转载请注明原文链接:https://www.cnblogs.com/nicole-zhang/p/14420044.html