Problems with pandas read csv

I have a problem with pandas read_csv. I had a lot of txt files that are associated with the stock market. Like this:

SecCode,SecName,Tdate,Ttime,LastClose,OP,CP,Tq,Tm,Tt,Cq,Cm,Ct,HiP,LoP,SYL1,SYL2,Rf1,Rf2,bs,s5,s4,s3,s2,s1,b1,b2,b3,b4,b5,sv5,sv4,sv3,sv2,sv1,bv1,bv2,bv3,bv4,bv5,bsratio,spd,rpd,depth1,depth2 600000,ζ΅¦ε‘ι“Άθ‘Œ,20120104,091501,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.600,8.600,.000,.000,.000,.000,0,0,0,0,1100,1100,38900,0,0,0,.00,.000,.00,.00,.00 600000,ζ΅¦ε‘ι“Άθ‘Œ,20120104,091506,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,33605,0,0,0,.00,.000,.00,.00,.00 600000,ζ΅¦ε‘ι“Άθ‘Œ,20120104,091511,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,34605,0,0,0,.00,.000,.00,.00,.00 600000,ζ΅¦ε‘ι“Άθ‘Œ,20120104,091551,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,56795,56795,35205,0,0,0,.00,.000,.00,.00,.00 600000,ζ΅¦ε‘ι“Άθ‘Œ,20120104,091621,8.490,.000,.000,0,.000,0,0,.000,0,.000,.000,.000,.000,.000,.000, ,.000,.000,.000,.000,8.520,8.520,.000,.000,.000,.000,0,0,0,0,57795,57795,34205,0,0,0,.00,.000,.00,.00,.00 

while I use this code to read it:

 fields = ['SecCode', 'Tdate','Ttime','LastClose','OP','CP','Rf1','Rf2'] df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields) 

But I had a problem:

 Traceback (most recent call last): File "E:/workspace/Senti/highlevel/highlevel.py", line 8, in <module> df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields,header=1) File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 562, in parser_f return _read(filepath_or_buffer, kwds) File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 315, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 645, in __init__ self._make_engine(self.engine) File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 799, in _make_engine self._engine = CParserWrapper(self.f, **self.options) File "D:\Anaconda2\lib\site-packages\pandas\io\parsers.py", line 1257, in __init__ raise ValueError("Usecols do not match names.") ValueError: Usecols do not match names. 

I can not find a problem similar to mine. And also it was connected, when I copy the txt file to another, the code works fine, but the source one causes the above problem. How can I solve it?

+5
source share
2 answers

In your post, you said you were working:

 df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields) 

Which did not give an error for me and @Anil_M. But from your trace, you can see that the command used is another:

 df = pd.read_csv('SHL1_TAQ_600000_201201.txt',usecols=fields, header=1) 

which includes header=1 , and it throws the specified error.

So, I would suggest that the error is due to some confusion in your code.

+2
source

Use names instead of usecols when specifying a parameter.

0
source

Source: https://habr.com/ru/post/1258484/


All Articles