Pandas GroupBy.apply method duplicates first group

后端 未结 3 1681
忘掉有多难
忘掉有多难 2020-11-22 10:41

My first SO question: I am confused about this behavior of apply method of groupby in pandas (0.12.0-4), it appears to apply the function TWICE to the first row of a data fr

3条回答
  •  粉色の甜心
    2020-11-22 11:19

    you can use for loop to avoid the groupby.apply duplicate first row,

    log_sample.csv

    guestid,keyword
    1,null
    2,null
    2,null
    3,null
    3,null
    3,null
    4,null
    4,null
    4,null
    4,null
    

    my code snippit

    df=pd.read_csv("log_sample.csv") 
    grouped = df.groupby("guestid")
    
    for guestid, df_group in grouped:
        print(list(df_group['guestid'])) 
    
    df.head(100)
    

    output

    [1]
    [2, 2]
    [3, 3, 3]
    [4, 4, 4, 4]
    

提交回复
热议问题