我将日期列转换为月份和日期的两个日期时间列。然后,我尝试根据月份列以及该列的计算方式进行过滤。我一直收到触发另一个异常的TypeError消息。
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['months'] = df['Start Time'].dt.month #strftime("%B")
df['day_of_week'] = df['Start Time'].dt.weekday_name
if day != 'all':
df = df.day_of_week.str.contains(day.title())
if month != 'all':
month = month.title()
months = ['January', 'February', 'March', 'April', 'May', 'June']
month = months.index(month) + 1
#filter by month to create the new dataframe
df = df[df['months'] == month]
return df
popular_month = df['months'].mode()[0]
我希望函数返回一个由“ month”的值过滤的数据帧,并让另一行代码返回最流行的月份。我收到此错误:
Traceback (most recent call last):
File "C:something/yet_another.py", line 308, in <module>
main()
File "C:something/yet_another.py", line 289, in main
df = load_data(city, month, day)
File "C:/something/yet_another.py", line 133, in load_data
df = df[df['months'] == month]
File "C:something\venv\lib\site-packages\pandas\core\series.py", line 1068, in __getitem__
result = self.index.get_value(self, key)
File "C:\soemthing\venv\lib\site-packages\pandas\core\indexes\base.py", line 4730, in get_value
return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 88, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 128, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index_class_helper.pxi", line 91, in pandas._libs.index.Int64Engine._check_type
KeyError: 'months'
答案 0 :(得分:0)
这是导致您出现问题的行:
df = df.day_of_week.str.contains(day.title())
您正在用df
中的Series
覆盖True, False
。