Cumulative operations on dtype objects

后端 未结 3 1168
清歌不尽
清歌不尽 2020-12-19 09:02

I am trying to figure out how I can apply cumulative functions to objects. For numbers there are several alternatives like cumsum and cumcount. The

3条回答
  •  星月不相逢
    2020-12-19 09:25

    well, you can define a custom function

    def custom_cumsum(df):
        from functools import reduce
        nrows, ncols = df.shape
        index, columns = df.index, df.columns
        rets = {}
        new_col = None
        for col in df.columns:
            try:
                new_col = {col:df.loc[:, col].cumsum()}
            except TypeError as e:
                if 'set' in str(e):
                    new_col = {col:[ reduce(set.union, df.loc[:, col][:(i+1)]) for i in range(nrows)]}
            rets.update(new_col)
        frame = pd.DataFrame(rets, index=index, columns=columns)
        return frame
    

提交回复
热议问题