Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

Can anybody explain why is loc used in python pandas with examples like shown below?

for i in range(0, 2):

  for j in range(0, 3):

    df.loc[(df.Age.isnull()) & (df.Gender == i) & (df.Pclass == j+1),

            'AgeFill'] = median_ages[i,j]

1 Answer

0 votes
by (33.1k points)

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!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...