如何在Python中修复此错误(AttributeError:'Int64Index'对象没有属性'dayofweek'

2024-04-16 21:58:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用pandas的get_dummies功能来获取一周中某一天的假人

days_of_week = pd.get_dummies(df.index.dayofweek, prefix='weekday',
drop_first=True)

我得到了这个错误:AttributeError: 'Int64Index' object has no attribute 'dayofweek'

有人能帮忙吗


Tags: of功能pandasdfgetprefixindexdays
1条回答
网友
1楼 · 发布于 2024-04-16 21:58:42

问题是您的索引不是DateTimeIndex。“dayofweek”属性对于整数索引不可用。首先需要将索引转换为DateTime并应用此代码。如果您有标准格式的日期,可以这样做:

df.index = pd.to_datetime(df.index)

相关问题 更多 >