understanding MultiIndex

后端 未结 2 906
情书的邮戳
情书的邮戳 2021-01-06 06:15

So I have a sample data set like this in csv:-

name    team    date       score
John    A   3/9/12      100
John    B   3/9/12      99
Jane    B   4/9/12             


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 06:47

    Figured it out myself.

    df = read_csv("data/Workbook1.csv")
    
    df
    
         name team      date  score
    0    John    A    3/9/12    100
    1    John    B    3/9/12     99
    2    Jane    B    4/9/12    102
    3   Peter    A    9/9/12    103
    4   Josie    C   11/9/12    111
    5  Rachel    A  30/10/12     98
    6    Kate    B  31/10/12    103
    7   David    C   1/11/12    104
    
    df2 = df.pivot('team', 'name').stack()
    
    df2
    
                     date  score
    team name                   
    A    John      3/9/12    100
         Peter     9/9/12    103
         Rachel  30/10/12     98
    B    Jane      4/9/12    102
         John      3/9/12     99
         Kate    31/10/12    103
    C    David    1/11/12    104
         Josie    11/9/12    111
    

提交回复
热议问题