简体   繁体   中英

add group property in pandas within a chain (analogous to dplyr group_by - mutate in R)

I would like to add some group properties as a new column to a pandas dataframe but without beaking the chain. I know this is possible in R using dplyr but I cannot get it to work in pandas.

The dplyr code would be (for adding max of column B per group in column A):

df %>%
   group_by(A) %>%
   mutate(max = max(B)) %>%
   ungroup() %>%
   ... more operations

The only way I can get it to work in pandas is:

df['max'] = df.groupby('A')['B'].transform('max')

but this requires a seperate line to assign the new column while I would like to do it inside a chain. Any help would be appreciated.

df.assign(max=df.groupby('A')['B'].transform('max'))....more operations

Now you are able to do it smoothly with datar

from datar import f
from datar.base import max
from datar.dplyr import group_by, mutate, ungroup

df >> \
   group_by(f.A) >> \
   mutate(max = max(f.B)) >> \
   ungroup() # >> 
   # ... more operations

I am the author of the package. Feel free to submit issues if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM