【发布时间】:2021-04-19 04:42:03
【问题描述】:
我有一个数据框df 如下:
Student_ID Subject Weekly_Feedback Feedack_questions Exam_Phase
100101 Physics Excellent: 5 How was physics class? Mid term
100101 NaN NaN how was physics class? Final
100101 Physics Good: 3 How was the Presentation? Final
100101 NaN Excellent: 5 How Much would you rate? Mid term
100102 NaN NaN how much would you rate? Mid term
100102 Chemistry Good: 3 How was chemistry class? Final
100102 NaN Good: 3 How was over all experience? Mid term
100102 NaN very Good: 3 How was topics explained? Final
100102 NaN NaN How did the class go? Mid term
100103 Mathematics NaN NaN Final
100103 Mathematics Good :4 NaN Final
我想以这样一种方式总结数据,对于每个Student_ID,必须只有一个记录(行),每个记录(行)都包含Exam_Phase 作为Final和Mid Term,如果一个Stundet_ID 只有一个Exam_Phase,那么只需要考虑那个。必须选择行以尽可能包含最少的NaN
(例如:在数据框的最后4行,即:
Student_ID Subject Weekly_Feedback Feedack_questions Exam_Phase
100102 Chemistry Good: 3 How was chemistry class? Final
100102 NaN Good: 3 How was over all experience? Mid term
100102 NaN very Good: 3 How was topics explained? Final
100102 NaN NaN How did the class go? Mid Term
对于Student_ID = 100102,此行:
100102 NaN Good: 3 How was over all experience? Mid term
必须选择,因为当Exam_Phase = Mid term 时,该行的NaN 值较少(仅在其中一列中)。 )
输出数据框应如下所示:
Student_ID Subject Weekly_Feedback Feedack_questions Exam_Phase
100101 Physics Excellent: 5 How was physics class? Mid term
100101 Physics Good: 3 How was the Presentation? Final
100102 Chemistry Good: 3 How was chemistry class? Final
100102 NaN Good: 3 How was over all experience? Mid term
100103 Mathematics Good :4 NaN Final
【问题讨论】:
标签: python python-3.x pandas dataframe nan