When I use groupby().apply() function to calculate some data like wighted average. I found that the first group is always calculated twice. For example:
def test(dataframe):
df = dataframe.copy()
a = df['a'].iloc[0]
b = df['b'].mean()
result.append([a,b])
df = pd.DataFrame({'a':[1,1,1,2,2,2,2,3,3,3],'b':[1,2,3,4,5,6,7,8,9,10]})
df.groupby('a').apply(test)
result = pd.DataFrame(result, columns=['a', 'b'])
As you can see, the first group is calculated twice. I don't know why.