gpt4 book ai didi

python - 在 Pandas 中使用 read_csv 时使用 skiplines 时出错

转载 作者:行者123 更新时间:2023-11-28 21:15:20 24 4
gpt4 key购买 nike

我正在尝试使用 Pandas 库中的 read_csv 将数据框导入 Python。数据文件 (annual_precip.csv) 的顶部如下所示:

Average annual precipitation and land area ,,
,mm/year,thousand km^2
country,precip,area
Afghanistan,327,652.2
Albania,1485,27.4
Algeria,89,2381.7
American Samoa,,0.2

这是我的代码:

from pandas import read_csv
read_csv('annual_precip.csv', index_col = [0], skiprows = 2)

这会产生以下错误:

Traceback (most recent call last):`

File "<ipython-input-894-742b462476f6>", line 1, in <module>
rain =read_csv('exploratory_computing_with_python/notebook5/annual_precip.csv', skiprows = 2, index_col = [0])`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 465, in parser_f
return _read(filepath_or_buffer, kwds)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 241, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 557, in __init__
self._make_engine(self.engine)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 694, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)`

File "/Users/jakoberickson/anaconda/lib/python2.7/site-packages/pandas/io/parsers.py", line 1061, in __init__
self._reader = _parser.TextReader(src, **kwds)`

File "pandas/parser.pyx", line 512, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4804)`

ValueError: No columns to parse from file`

当我跳过 1 行或 0 行时,我没有收到任何错误,但我的列名不正确。如果有什么不同的话,我在优胜美地的 Anaconda 包中运行 Spyder 2.3.4。

最佳答案

它适用于您示例中的 csv 示例,但您的问题是您的 csv 文件包含不可打印的字符。

试试这个:

df = pd.read_csv('annual_precip.csv', index_col = [0],  encoding='utf-8', skiprows = 2)

关于python - 在 Pandas 中使用 read_csv 时使用 skiplines 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30412683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com