【问题标题】:Problems while trying to read a csv with pandas?尝试使用 pandas 读取 csv 时出现问题?
【发布时间】:2023-04-05 20:38:01
【问题描述】:

我有一个如下所示的 csv 文件:

Id, text, label
10101, string, label

然后我想用熊猫放入一个数据框,所以我这样做:

 df = pd.read_csv('/path/.csv')
 X, y = df['text'], df['label']

我得到了这个回溯:

Traceback (most recent call last):
  File "/Users/user/test.py", line 27, in <module>
    X, y, = df['text'], df['label']
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 1780, in __getitem__
    return self._getitem_column(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 1787, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/generic.py", line 1058, in _get_item_cache
    values = self._data.get(item)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 2889, in get
    loc = self.items.get_loc(item)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/index.py", line 1400, in get_loc
    return self._engine.get_loc(_values_from_object(key))
  File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3807)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3687)
  File "pandas/hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12310)
  File "pandas/hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12261)
KeyError: 'text'

谁能帮助我了解正在发生的事情以及如何使用 pandas 正确读取此文件?提前谢谢大家。

【问题讨论】:

  • X, y, = df['text'], df['label'],这不应该是X, y = df['text'], df['label']
  • 抱歉,我编辑了感谢@Anmol_uppal 的观察!

标签: python python-2.7 csv pandas io


【解决方案1】:

CSV 文件中的标题为:

Id, text, label

请注意,第 2 列和第 3 列的列标题中有前导空格。您可以通过包含空格来访问该列:

x, y = df[' text'], df[' label']

或者指定skipinitialspace参数:

df = pd.read_csv('/path/x.csv', skipinitialspace=True)
x, y = df['text'], df['label']

后者也会从列数据中删除初始空格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2020-07-20
    • 2022-12-31
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多