We have a dataframe like
Out[90]:
customer_id created_at
0 11492288 2017-03-15 10:20:18.280437
1 8953727 2017-03-16 12:51:00.145629
2 11492288 2017-03-15 10:20:18.284974
3 11473213 2017-03-09 14:15:22.712369
4 9526296 2017-03-14 18:56:04.665410
5 9526296 2017-03-14 18:56:04.662082
I would like to create a new column here based on customer_id groups, random 8 character rows assigned to each group.
For example, the result will look like
Out[90]:
customer_id created_at code
0 11492288 2017-03-15 10:20:18.280437 nKAILfyV
1 8953727 2017-03-16 12:51:00.145629 785Vsw0b
2 11492288 2017-03-15 10:20:18.284974 nKAILfyV
3 11473213 2017-03-09 14:15:22.712369 dk6JXq3u
4 9526296 2017-03-14 18:56:04.665410 1WESdAsD
5 9526296 2017-03-14 18:56:04.662082 1WESdAsD
I use R and dplyr, and it is very simple to write this transformation using them. I am looking for something similar in Pandas for this:
library(dplyr)
library(stringi)
df %>%
group_by(customer_id) %>%
mutate(code = stri_rand_strings(1, 8))
I can figure out the random character part. Just wondering how Pandas groupby works in this case.
Thank!
source
share