I think I understand your question now, but then I don't see how you got the '3' for row 6 col A. I am following the logic for how I was able to match the 3's in rows 1 and 2 in col A, which I will try to explain in code below. If this isn't quite the correct interpretation, hopefully is still gets you pointed in the right direction.
Your initial df
df = pd.DataFrame({
    'A': [0, 0, 9, 10, 8, 0, 0], 
    'B': [7, 7, 7, 6, 6, 6, 2]
    })
    A   B
1   0   7
2   0   7
3   9   7
4   10  6
5   8   6
6   0   6
7   0   2
Restating the objective:
For each unique value in col B where col A is 0, find the rows in col A where B has that value and take the mean of those col A values. Then overwrite that mean value to those rows in A that are 0 and line up with the value selected in B. So, for example, the first 3 rows there are 7's in col B, and 0, 0, 9 in col A. The mean of the first 3 A rows is 3, so that is the value that will get overwritten on the 0s in col A, rows 1 and 2.
Steps
Get the unique values from col B where col A is also 0
bvals_when_a_zero = df[df['A'] == 0]['B'].unique()
array([7, 6, 2])
For each of those unique values, calculate the mean of the corresponding values in col A
means = [df[df['B'] == i]['A'].mean() for i in bvals_when_a_zero]
[3.0, 6.0, 0.0]
Loop over the bvals,means pairs and overwrite the 0's with the corresponding mean for the bval. The logic of the pandas where method keeps the values stated to the left (in this case, df['A'] values) that meet the condition in the first argument in the brackets, otherwise choose the second argument as the value to keep. Our condition (df['A'] == 0) & (df['B'] == bval) says get the rows where col A is 0 and col B is one of the unique bvals. But here we actually want to keep the df['A'] values that are NOT equal to the condition, so the conditional argument in the brackets is negated with the ~ symbol in front.
for bval, mean in zip(bvals_when_a_zero, means):
    df['A'] = df['A'].where( ~((df['A'] == 0) & (df['B'] == bval)), mean )
This gives the final df
    A   B
1   3   7
2   3   7
3   9   7
4   10  6
5   8   6
6   6   6
7   0   2