解决pandas.errors.InvalidIndexError: (slice(None, None, None), None)

Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 142, in pandas._libs.index.IndexEngine.get_loc
TypeError: '(slice(None, None, None), None)' is an invalid key

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\matplotlib\cbook\__init__.py", line 1318, in _check_1d
    ndim = x[:, None].ndim
  File "D:\Anaconda\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__
    indexer = self.columns.get_loc(key)
  File "D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 3628, in get_loc
    self._check_indexing_error(key)
  File "D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 5637, in _check_indexing_error
    raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: (slice(None, None, None), None)

ValueError: x and y must have same first dimension, but have shapes (2, 14) and (14,)
在这里插入图片描述
问题分析:
由于上图索引(时间列)存在重复,导致下面代码中, ax.plot()的时候,把两个相同的索引,构造了一个(2,14)的重复

fig, ax = plt.subplots(figsize=(15, 7))
cmap = plt.get_cmap('viridis')
colors = cmap(np.linspace(0, 1, len(df_10minv2)))
for i, (name, color) in tqdm(enumerate(zip(names, colors), 1)):
    ax.plot(df_10minv2.loc[name], height, c=color, linestyle='-', label=str(name), marker='.')

解决办法:对索引列去重复

# 如果你想保留第一个aa,那么keep就是first
df.reset_index().drop_duplicates(subset='index', keep='first').set_index('index')
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值