Pandas transform() vs apply()

后端 未结 2 1792

I don\'t understand why apply and transform return different dtypes when called on the same data frame. The way I explained the two functions to my

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 02:09

    It looks like SeriesGroupBy.transform() tries to cast the result dtype to the same one as the original column has, but DataFrameGroupBy.transform() doesn't seem to do that:

    In [139]: df.groupby('id')['cat'].transform(lambda x: (x == 1).any())
    Out[139]:
    0    1
    1    1
    2    1
    3    1
    4    1
    5    1
    6    1
    7    0
    8    0
    9    1
    Name: cat, dtype: int64
    
    #                         v       v
    In [140]: df.groupby('id')[['cat']].transform(lambda x: (x == 1).any())
    Out[140]:
         cat
    0   True
    1   True
    2   True
    3   True
    4   True
    5   True
    6   True
    7  False
    8  False
    9   True
    
    In [141]: df.dtypes
    Out[141]:
    cat    int64
    id     int64
    dtype: object
    

提交回复
热议问题