Pandas Groupby Agg Function Does Not Reduce

后端 未结 2 581
日久生厌
日久生厌 2020-12-01 18:14

I am using an aggregation function that I have used in my work for a long time now. The idea is that if the Series passed to the function is of length 1 (i.e. the group only

2条回答
  •  独厮守ぢ
    2020-12-01 18:52

    I can't really explain you why, but from my experience list in pandas.DataFrame don't work all that well.

    I usually use tuple instead. That will work:

    def MakeList(x):
        T = tuple(x)
        if len(T) > 1:
            return T
        else:
            return T[0]
    
    DF_Agg = DFGrouped.agg({'s.m.v.' : MakeList})
    
         date line_code           s.m.v.
    0  2013-04-02    401101   (7.76, 25.564)
    1  2013-04-02    401102           25.564
    2  2013-04-02    401103             9.55
    3  2013-04-02    401104             4.87
    4  2013-04-02    401105   (7.76, 25.564)
    5  2013-04-02    401106  (5.282, 25.564)
    6  2013-04-02    401107            5.282
    

提交回复
热议问题