I want to group my data by two different variables and then create a new column which state whether or nor "3" or "4" appears in the grouping, but I keep getting different types of errors. Here is my sample data:
d = {'id' : ["A","A","A","A","A","A","B","B","B","B","B","B"],
'month' : [1,1,1,1,2,2,1,1,1,1,2,2],
'week' : [1,2,3,4,1,2,1,2,3,4,1,2]}
example_df = pd.DataFrame(data = d)
I want to group by id and month and then within that grouping, create a new column titled has_3_or_4 when 3 or 4 appear in the column week
example_df = (example_df.assign(has_3_or_4 = example_df.groupby(['id', 'month'])
.apply(lambda x: np.where(any(x.week.isin([3, 4])),'has_3_or_4', 'no_3_or_4'))))
But this returns this error:
TypeError: incompatible index of inserted column with frame index
I googled this and found a solution which says to include as_index = False within the groupby, which does indeed allow the code to run without any error, but the end result is not what I want, it just looks weird.
Does anyone have any idea how to do this? in R it is so straightforward - e.g. example_df %>% group_by(id, month) %>% mutate(has_3_or_4 = if_else(any(c(3,4) %in% week), 'has_3_or_4', 'no_3_or_4'))