I am trying to convert the following block of codes written in R to Python:
df <- df %>%
group_by("column_1") %>%
mutate(new_col1 = length(which(column_x < 1)),
new_col2 = new_col1 /counter)
df: is a dataframe
My attempt to do this in Python is the following blocks:
df = df.groupby(['column_1']).apply(
new_col1=len(df[df['column_x']] < 1)),
new_col2= df['new_col1'] / num_samples)
But I am getting the following error:
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
Note that column new_col2 needs new_col1 to be created and so I couldn't find a way to combine the operation of creating two columns with custom behavior and group them by a single column from the data frame.
How would I able to convert the above R block of codes into a working python code using pandas?
Thanks a lot in advance,