解决 groupby 出现错误:TypeError: '<' not supported between instances of 'int' and &

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)

 成功执行!!

上一篇:Python 玩转数据 - Pandas 数据处理 合并 pd.merge() df1.merge(df2)


下一篇:活动安排(贪心)