解决 groupby 出现错误:TypeError: ‘<' not supported between instances of 'int' and 'datetime.datetime'(An error occurred while resolving groupby: typeerror: '<' not supported between instances of 'Int' and 'datetime. Datetime')

g = df2.groupby([‘name’])for label, option_course in g:     #其中key代表分组后字典的键,也就是score    print(label)    #字典对应的值选修的科目    print(option_course)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘datetime.datetime’

错误原因:在name中,某些元素被识别成了 int 和 datetime

解决方法:使用astype全部改为同一种类型

df2[‘name’] = df2[‘name’].astype(str)

g = df2.groupby([‘name’])for label, option_course in g:     #其中key代表分组后字典的键,也就是score    print(label)    #字典对应的值选修的科目    print(option_course)

 成功执行!!

————————

g = df2.groupby([‘name’])for label, option_ course in g:      # Where key represents the key of the dictionary after grouping, that is, score     print(label)     # Elective subject corresponding to the value of the dictionary     print(option_course)

TypeError: ‘<‘ not supported between instances of ‘int’ and ‘datetime.datetime’

Error reason: in name, some elements are recognized as int and datetime

Solution: use astype to change all to the same type

df2[‘name’] = df2[‘name’].astype(str)

g = df2.groupby([‘name’])for label, option_ course in g:      # Where key represents the key of the dictionary after grouping, that is, score     print(label)     # Elective subject corresponding to the value of the dictionary     print(option_course)

Successfully executed!!