202103-30 pandas 实现分组后取第N行 目的:把question_id对应的user_answer转成ABCDsolutiondfa=df.groupby('question_id').nth(0).reset_index()dfa['flag']='A'dfb=df.groupby('question_id').nth(1).reset_index()dfb['flag']='B'dfc=df.groupby('question_id').nth(2).reset_index()dfc['flag']='C'dfd=df.groupby('question_id').nth(3).reset_index()dfd['flag']='D'resdf=dfa.append([dfb,dfc,dfd])resdf.sort_values(by='question_id')result:focus:g.nth(... 继续阅读 >