what is different between groupby.first, groupby.nth, groupby.head when as_index=False

后端 未结 2 902
孤城傲影
孤城傲影 2020-12-21 07:01

Edit: the rookie mistake I made in string np.nan having pointed out by @coldspeed, @wen-ben, @ALollz. Answers are quite good, so I don\'t d

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 07:40

    Here is the different, you need to make the np.nan to NaN , in your original df it is string , after convert it , you will see the different

    df=df.mask(df=='np.nan')
    df.groupby('A', as_index=False).head(1) #df.groupby('A', as_index=False).nth(0)
    
    Out[8]: 
       A    B
    0  1  NaN
    3  2    8
    df.groupby('A', as_index=False).first() 
    # the reason why first have the index reset, 
    #since it will have chance select the value from different row within the group, 
    #when the first item is NaN it will skip it to find the first not null value 
    #rather than from the same row, 
    #If still keep the original row index will be misleading. 
    Out[9]: 
       A  B
    0  1  4
    1  2  8
    

提交回复
热议问题