Skip to content Skip to sidebar Skip to footer

Update Column Value Of Pandas Groupby().last()

Given dataframe: dfd = pd.DataFrame({'A': [1, 1, 2,2,3,3], 'B': [4, 5, 6,7,8,9], 'C':['a','b','c','c','d','e'] }) I can

Solution 1:

IIUIC, you need loc. Get the index of last values using tail

In [1145]: dfd.loc[dfd.groupby('A')['C'].tail(1).index, 'C'] = np.nan

In [1146]: dfd
Out[1146]:
   A  B    C
014    a
115  NaN
226    c
327  NaN
438    d
539  NaN

dfd.loc[dfd.groupby('A').tail(1).index, 'C'] = np.nan should be fine too.

Post a Comment for "Update Column Value Of Pandas Groupby().last()"