标签:matlab user 保存文件 oca reset pip and 报错 注意
5、读取带 ‘,‘分隔符的txt文件
import pandas as pd
import numpy as np from pandas import Series,DataFrame df=pd.DataFrame(np.arange(16).reshape(4,4),index=list(‘abcd‘),columns=list(‘xyzm‘)) df Out[9]: x y z m a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 d 12 13 14 15
5、读取带 ‘,‘分隔符的txt文件
注意:如果出现编码错误,可以将txt文档另存为utf-8格式
import pandas as pd data=pd.read_table(‘jingzhi.txt‘,parse_dates=True,sep=r‘,‘) #设置文档采用分隔符 data.to_csv(‘data.csv‘,index=False) # 用python写CSV、EXCEL文件 with open(‘abc.csv‘,‘a‘) as f: df=pd.DataFrame([[1,2],[3,4]],index=list(‘ab‘)) df.to_csv(f,mode=‘a‘,header=None)
这个问题比较坑,处理办法可以重新生成新的DataFrame,把数据保存到新的DataFrame中去。
import pandas as pd import numpy as np from pandas import Series,DataFrame df=pd.DataFrame(np.arange(16).reshape(4,4),index=list(‘abcd‘),columns=list(‘xyzm‘)) Backend Qt5Agg is interactive backend. Turning interactive mode on. df Out[10]: x y z m a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 d 12 13 14 15 df_part=df.loc[‘a‘:‘c‘,:] df_part Out[12]: x y z m a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 df_part[‘n‘]=np.nan D:\Program Files\JetBrains\PyCharm Community Edition 2017.3\helpers\pydev\pydevconsole.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy ‘‘‘
File "D:\Anaconda3\lib\shutil.py", line 393, in _rmtree_unsafe onerror(os.rmdir, path, sys.exc_info()) File "D:\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 114, in rmtree_errorhandler func(path) PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: ‘C:\\Users\\matlab\\AppData\\Local\\Temp\\pip-build-r8bkas1s\\pendulum‘
import tushare as ts trade_date=ts.trade_cal() trade_date=trade_date[trade_date[‘isOpen‘]==1][‘calendarDate‘] trade_date Out[23]: 0 1990-12-19 1 1990-12-20 2 1990-12-21 5 1990-12-24 6 1990-12-25 7 1990-12-26
#索引重排列
trade_date=trade_date.reset_index(drop=True)
trade_date
Out[26]:
0 1990-12-19
1 1990-12-20
2 1990-12-21
3 1990-12-24
4 1990-12-25
5 1990-12-26
保存文件位置:
E:\wind_get_data\date\trade_date2018.csv
标签:matlab user 保存文件 oca reset pip and 报错 注意
原文地址:https://www.cnblogs.com/bawu/p/8662975.html