How to get mean of column using groupby() and another condition For the following

df = pd.DataFrame()
df['1'] = 1,2,1,2,1,2
df['2'] = 3,6,5,4,7,8
df['3'] = 1,1,1,2,2,2


I want to do 

mean(df.groupby().loc[df['1']==df['3'],'2'].mean()


which doesn't work. And simply doing this :

a=df.groupby(['1','3'])['2'].mean()


gives

OUTPUT: 
1  3
1  1    4
   2    7
2  1    6
   2    6


But I only want cases where column 1 and 3 have the same elements:

1  3
1  1    4
2  2    6
Try this way:

a=df[df['1']==df['3']].groupby(['1','3'])['2'].mean()