鱼C论坛

 找回密码
 立即注册
查看: 370|回复: 1

对列表切片操作报错

[复制链接]
发表于 2024-10-30 15:40:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #合并训练集和测试集
  2. #train_dataset.drop(['Id','SalePrice'],axis=1)
  3. full_dataset = pd.concat([train_dataset.drop(['Id','SalePrice'],axis=1),test_dataset[:,1:]])
  4. #train_dataset.shape,test_dataset.shape,full_dataset.shape
复制代码


报错如下
  1. ---------------------------------------------------------------------------
  2. TypeError                                 Traceback (most recent call last)
  3. File /opt/conda/lib/python3.10/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key)
  4.    3804 try:
  5. -> 3805     return self._engine.get_loc(casted_key)
  6.    3806 except KeyError as err:

  7. File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()

  8. File index.pyx:173, in pandas._libs.index.IndexEngine.get_loc()

  9. TypeError: '(slice(None, None, None), slice(1, None, None))' is an invalid key

  10. During handling of the above exception, another exception occurred:

  11. InvalidIndexError                         Traceback (most recent call last)
  12. Cell In[58], line 3
  13.       1 #合并训练集和测试集
  14.       2 #train_dataset.drop(['Id','SalePrice'],axis=1)
  15. ----> 3 full_dataset = pd.concat([train_dataset.drop(['Id','SalePrice'],axis=1),test_dataset[:,1:]])
  16.       4 #train_dataset.shape,test_dataset.shape,full_dataset.shape

  17. File /opt/conda/lib/python3.10/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key)
  18.    4100 if self.columns.nlevels > 1:
  19.    4101     return self._getitem_multilevel(key)
  20. -> 4102 indexer = self.columns.get_loc(key)
  21.    4103 if is_integer(indexer):
  22.    4104     indexer = [indexer]

  23. File /opt/conda/lib/python3.10/site-packages/pandas/core/indexes/base.py:3817, in Index.get_loc(self, key)
  24.    3812     raise KeyError(key) from err
  25.    3813 except TypeError:
  26.    3814     # If we have a listlike key, _check_indexing_error will raise
  27.    3815     #  InvalidIndexError. Otherwise we fall through and re-raise
  28.    3816     #  the TypeError.
  29. -> 3817     self._check_indexing_error(key)
  30.    3818     raise

  31. File /opt/conda/lib/python3.10/site-packages/pandas/core/indexes/base.py:6059, in Index._check_indexing_error(self, key)
  32.    6055 def _check_indexing_error(self, key):
  33.    6056     if not is_scalar(key):
  34.    6057         # if key is not a scalar, directly raise an error (the code below
  35.    6058         # would convert to numpy arrays and raise later any way) - GH29926
  36. -> 6059         raise InvalidIndexError(key)

  37. InvalidIndexError: (slice(None, None, None), slice(1, None, None))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-30 15:40:37 | 显示全部楼层
在你的代码中,`train_dataset.drop(,axis=1)` 这一行缺少了要删除的列名或列索引,这导致了错误。`drop` 方法需要一个明确的列名或索引来删除相应的列。

你可以这样修改代码:

  1. # 假设你想删除名为 'column_name' 的列
  2. full_dataset = pd.concat([train_dataset.drop('column_name', axis=1), test_dataset])
复制代码


如果你不想删除任何列,只是想合并数据集,可以直接使用:

  1. full_dataset = pd.concat([train_dataset, test_dataset])
复制代码


确保在 `drop` 方法中提供正确的列名或索引。如果你不确定列名,可以使用 `train_dataset.columns` 查看所有列名。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 16:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表