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
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