Given this DataFrame
df = pd.DataFrame({'Group': {0: 'a', 1: 'a', 2: 'b', 3: 'b', 4: 'c', 5: 'c', 6: 'd', 7: 'd'},
'Time': {0: 0, 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 7: 1},
'Mean': {0: 1, 1: 4, 2: 1, 3: 5, 4: 2, 5: 6, 6: 2, 7: 9}})
For each group in the DataFrame (a and b), I would like to take each number in the Mean column and divide it by the corresponding value at Time == 0, and take the np.log2 of this. In other words, I want to end up with this
df2 = pd.DataFrame({'Group': {0: 'a', 1: 'a', 2: 'b', 3: 'b', 4: 'c', 5: 'c', 6: 'd', 7: 'd'},
'Time': {0: 0, 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 7: 1},
'Mean': {0: 1, 1: 4, 2: 1, 3: 5, 4: 2, 5: 6, 6: 2, 7: 9},
'New': {0: 0.0, 1: 2.0, 2: 0.0, 3: 2.321928094887362, 4: 0.0, 5: 1.584962500721156, 6: 0.0, 7: 2.169925001442312}})
Currently I achieved this in the following way
df2 = pd.DataFrame()
for group, sub in df.groupby('Group'):
sub['New'] = np.log2(sub.Mean / sub.Mean.iloc[0])
df2 = pd.concat([df2, sub], axis=0)
But I feel like there should be a simpler way.