update column value of pandas groupby().last()

2019-07-31 17:08发布

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 find the last C value of each A group by using

dfd.groupby('A').last()['C']

However, I want to update the C values to np.nan. I don't know how to do that. Method such as:

def replace(df):
    df['C']=np.nan
    return replace

dfd.groupby('A').last().apply(lambda dfd: replace(dfd))

Does not work.

I want the result like:

dfd_result= pd.DataFrame({'A': [1, 1, 2,2,3,3],
                    'B': [4, 5, 6,7,8,9],
                    'C':['a',np.nan,'c',np.nan,'d',np.nan]
                   })

1条回答
Deceive 欺骗
2楼-- · 2019-07-31 18:02

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.

查看更多
登录 后发表回答