I am trying to perform simple calculations on a Pandas dataframe. Refer to the example below:
df = pd.DataFrame({'A': [i < 5 == 0 for i in range(0, 10)],
'B': [i for i in range(0, 10)]})
In this example, I want to create a new column 'C' that adds a group-specific counter offset to each value in column B so that I end up with df equaling:
A B C 0 True 0 1 1 True 1 2 2 True 2 3 3 True 3 4 4 True 4 5 5 False 5 1 6 False 6 2 7 False 7 3 8 False 8 4 9 False 9 5
I have tried the following code but I don't get the results I want:
def add_1(b_column):
return [i for i in range(0, len(b_column))]
df['C'] = df.groupby('A').apply(add_1)
Please advise.
Thanks