In your case, the .loc method is needed because of the method df.Age.isnull(), df.Gender == i and df.Pclass == j+1 may return a view of slices of the data frame or may return a copy. This may be ambiguous.
Without .loc, you end up calling all 3 conditions in series which may cause chained indexing. When you use .loc, you can access all your conditions in one step and pandas is no longer confused.
For more details on Pandas, study Python Certification.
Hope this answer helps you!