当使用groupby并使用apply函数进行reindex操作的时候,报错“cannot reindex from a duplicate axis”
此时应该检查index索引是否有重复的:
df.index.duplicated()#这个是按行查,比较准确
比如我的情况是,index列出现

两个相同的。
重新设定日期:
data["dh"] = data["date"].map(str) + '-' + data["hour"].map(str)
data['dh'] = pd.to_datetime(data['dh'], format="%Y-%m-%d-%H")

本文介绍在使用pandas的groupby与apply函数时遇到的“cannot reindex from a duplicate axis”错误,并提供了解决方案,包括如何检查索引重复及正确设置日期格式。
1万+

被折叠的 条评论
为什么被折叠?



