Pandas DataFrame.to_csv给出无效的切片错误

时间:2016-01-17 12:01:07

标签: python csv pandas export

我对导致此错误的原因感到有些困惑。一个非常简单的例子:

import pandas
df = pandas.DataFrame({"var1" : [1], "var2": [5]})
df.to_csv("csv_file.csv")

给我以下内容:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 1136, in to_csv
    formatter.save()
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1171, in save
    self._save()
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1277, in _save
    self._save_chunk(start_i, end_i)
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1289, in _save_chunk
    date_format=self.date_format)
  File "/usr/lib/python3/dist-packages/pandas/core/internals.py", line 552, in to_native_types
    values = values[:, slicer]
IndexError: invalid slice

之前有其他人遇到此错误吗?

1 个答案:

答案 0 :(得分:1)

df的定义中有拼写错误。使用以下内容:

df = pandas.DataFrame({"var1" : [1], "var2": [5]})

:需要,而不是var2