解决pandas.errors.InvalidIndexError: (slice(None, None, None), None)
短信预约 -IT技能 免费直播动态提醒
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_locTypeError: '(slice(None, None, None), None)' is an invalid keyDuring 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就是firstdf.reset_index().drop_duplicates(subset='index', keep='first').set_index('index')
来源地址:https://blog.csdn.net/weixin_46713695/article/details/128138944
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341