Tag Archives: pandas.errors.EmptyDataError

Error reading file by pandas pandas.errors.EmptyDataError: no columns to parse from file

1. Problems encountered:

The source code for reading. CSV is as follows:

import pandas as pd

def main():
    aqi_data = pd.read_csv('china_city_aqi.csv')
    print(aqi_data.head(5))

if __name__ == "__main__":
    main()

The complete error information is as follows:

Traceback (most recent call last):
  File "D:/XXX/Python Learning/lect09/AQI_9.0.py", line 14, in <module>
    main()
  File "D:/XXX/Python Learning/lect09/AQI_9.0.py", line 10, in main
    aqi_data = pd.read_csv('china_city_aqi.csv')
  File "D:\XXX\Python Learning\lect09\venv\new\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "D:\XXX\Python Learning\lect09\venv\new\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "D:\XXX\Python Learning\lect09\venv\new\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "D:\XXX\Python Learning\lect09\venv\new\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "D:\XXX\Python Learning\lect09\venv\new\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 545, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

2. Solutions

Take a look pandas.read_ The official document of CSV may be related to

Engine: {C ‘,’python’}, optional

Parser engine to use. The C engine is faster while the python engine is currently more feature-complete.

So read_ Add engine = “Python” to the parameter of csv()

The above code is modified as follows:

import pandas as pd

def main():
    aqi_data = pd.read_csv('china_city_aqi.csv', engine='python')
    print(aqi_data.head(5))

if __name__ == "__main__":
    main()

Run successfully after modification!