如何使用groupby制作唯一的用户ID级别数据帧?

时间:2019-02-21 05:59:44

标签: python pandas

我有一个输入数据框,其ID为Visit11 Visit12 Visit13 Visit14 Visit15

1   Orange              
2   Orange              
2       Apple           
3   Grapes              
4   Apple               
5   Not Defined             
5       Apple           
6   Apple               
7   Banana              
7                   
7                   
7                   
7                   
7                   
7                   
8   Banana              
8       Apple           
8           Banana      
8               Apple   
8                   Banana
9                   
9                   
9                   
9   

我正在使用groupby来获得预期的输出,但它会将所有购买合并为1个单元格。我希望将购买分为不同的列,其中1行适合1位用户。预期输出应为


    ID  Visit11 Visit12 Visit13 Visit1Int4  Visit15
1   Orange              
2   Orange  Apple           
3   Grapes              
4   Apple               
5   Not Defined Apple           
6   Apple               
7   Banana              
8   Banana  Apple   Banana  Apple   Banana
9                   

1 个答案:

答案 0 :(得分:1)

我相信您需要:

RID   UserID  CourseID    SemID   SubjectID   UnitID  ScoredMarks TotalMarks  No_Attempts               CreatedDate ModifiedDate
  1     1021       109     3000        2006      100           30       100             1   2019-02-12 00:00:00.000 NULL
  2     1021       109     3000        2006      101           40       100             1   2019-02-18 00:00:00.000 NULL
  3     1021       109     3000        2006      102           85       100             1   2019-02-19 00:00:00.000 NULL
  4     1022       109     3000        2006      101           80       100             1   2019-02-19 00:00:00.000 NULL
  5     1022       109     3000        2006      100           75       100             1   2019-02-19 00:00:00.000 NULL

替代解决方案:

public CalculatePerform(int? Student_ID, int? CourseID, int? SemID, int? SubjectID)
{
   var ScoreCard = from i in dbcontext.Stu_Result
                   where i.UserID == Student_ID && 
                         i.CourseID == CourseID && 
                         i.SemID == SemID && 
                         i.SubjectID == SubjectID
}