Difference between Pandas .iloc and .loc function

The optimized data access methods are accessed by indexing off of the .loc and .iloc attributes. These two attributes allow label-based and position-based indexing respectively.

When we perform an index operation on the .iloc attribute, it does lookup based on index position (in this case pandas behaves similar to a Python list).

DataFrame operation:

.loc is supposed to be based on the index labels and not the positions. As such, it is analogous to Python dictionary-based indexing. Though it has some additional functionality, as it can accept boolean arrays, slices, and a list of labels (none of which work with a Python dictionary):

Series operation:

If you get confused by .loc and .iloc, remember that .iloc is based the index (starting with i) position. .loc is based on label (starting with l).

Popular Posts

Author

  • Naveen Pandey Data Scientist Machine Learning Engineer

    Naveen Pandey has more than 2 years of experience in data science and machine learning. He is an experienced Machine Learning Engineer with a strong background in data analysis, natural language processing, and machine learning. Holding a Bachelor of Science in Information Technology from Sikkim Manipal University, he excels in leveraging cutting-edge technologies such as Large Language Models (LLMs), TensorFlow, PyTorch, and Hugging Face to develop innovative solutions.

    View all posts
Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *