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

痴心易碎 提交于 2020-02-04 02:29:46

问题


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:


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.



来源:https://stackoverflow.com/questions/45846102/update-column-value-of-pandas-groupby-last

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!