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
0  1  4    a
1  1  5  NaN
2  2  6    c
3  2  7  NaN
4  3  8    d
5  3  9  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()"