解决 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)

 成功执行!!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM