recmdf = pd.DataFrame(columns=['courseid','recom1','recom2']) recmdf['idcourse'] =…
- User
recm_df = pd.DataFrame(columns=['course_id','recom_1','recom_2']) recm_df['id_course'] = course_pairs_df['course_id'].unique()
recm_df['recom_1'] = recm_df['course_id'].apply(lambda x: recommend(x)[0][0]) recm_df['recom_2'] = recm_df['course_id'].apply(lambda x: recommend(x)[0][0]) recm_df
возникает ошибка, как исправить? TypeError Traceback (most recent call last) Untitled-3.ipynb Ячейка 34 line 4 1 recm_df = pd.DataFrame(columns=['course_id','recom_1','recom_2']) 2 recm_df['id_course'] = course_pairs_df['course_id'].unique() ----> 4 recm_df['recom_1'] = recm_df['course_id'].apply(lambda x: recommend(x)[0][0]) 5 recm_df['recom_2'] = recm_df['course_id'].apply(lambda x: recommend(x)[0][0]) 6 recm_df
File c:\Users\Владимир\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py:4771, in Series.apply(self, func, convert_dtype, args, **kwargs) 4661 def apply( 4662 self, 4663 func: AggFuncType, (...) 4666 **kwargs, 4667 ) -> DataFrame | Series: 4668 """ 4669 Invoke function on values of Series. 4670 (...) 4769 dtype: float64 4770 """ -> 4771 return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
File c:\Users\Владимир\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\apply.py:1123, in SeriesApply.apply(self) ... ----> 4 if i[0] == course_id: 5 list_course.append((i,count_of_courses[i])) 6 if i[1] == course_id:
TypeError: 'int' object is not subscriptable
- Assistant
The error message suggests that the variable
course_idis an integer and not a subscriptable object. To fix this error, you need to make sure that thecourse_idvariable is a valid index for accessing therecommend()function.Here's one possible solution:
pythonrecm_df['recom_1'] = recm_df['id_course'].apply(lambda x: recommend(x)[0][0]) recm_df['recom_2'] = recm_df['id_course'].apply(lambda x: recommend(x)[0][1])In this solution, I assume that
id_courseis the correct column name in therecm_dfdataframe that contains the course ids. You may need to adjust this column name based on your specific dataframe structure. - User
recm_df = pd.DataFrame(columns=['course_id','recom_1','recom_2']) recm_df['id_course'] = course_pairs_df['course_id'].unique()
recm_df['recom_1'] = recm_df['course_id'].apply(lambda x: recommend(x)[0][0]) recm_df['recom_2'] = recm_df['course_id'].apply(lambda x: recommend(x)[1][0]) recm_df
TypeError: 'int' object is not subscriptable
как исправить?
- Assistant
The updated code snippet you provided still raises a
TypeError: 'int' object is not subscriptable. This error message typically occurs when you try to access an index or slice of an object that is not subscriptable, such as an integer.To fix this error, make sure that the
recommend()function accepts the correct input and returns the appropriate values.Here's an example of how you can modify the code:
pythonrecm_df['recom_1'] = recm_df['id_course'].apply(lambda x: recommend(x)[0] if recommend(x) else None) recm_df['recom_2'] = recm_df['id_course'].apply(lambda x: recommend(x)[1] if len(recommend(x)) > 1 else None)In this updated code, I assume that the
recommend()function returns a list with at least one recommendation. If the function doesn't return a recommendation for a specific course, I assignNoneto the corresponding column.Make sure to adapt the code according to your specific requirements and the behavior of the
recommend()function.