解決 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