Data Selection with LOC and ILOC in Pandas

Extract the bottom-right three-by-three subset of the DataFrame using both LOC and ILOC.

For this next part, I'd like you to get a little practice with loc and iloc. So our task is to get the last three rows and last three columns.

Practice selecting precise data subsets using Python's LOC and ILOC indexing methods. Gain hands-on experience extracting specific rows and columns from data frames.

Key Insights

  • Practice using Python's LOC and ILOC indexing methods by extracting the last three rows and columns from a data frame.
  • The targeted data subset includes columns named fuel efficiency, latest launch, and power performance factor.
  • Complete this indexing task twice—once utilizing LOC and once employing ILOC—to master the differences between these selection techniques.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Machine Learning Essentials

  • Supervised vs Unsupervised

    Labeled data vs unlabeled — different problem classes.

  • Classification vs Regression

    Predict a class label vs a continuous number.

  • Train/Test Split

    Always evaluate on data the model never saw during training.

  • Hyperparameter Tuning

    Grid search and cross-validation to find the best settings.

For this next part, I'd like you to get a little practice with loc and iloc. So our task is to get the last three rows and last three columns. As it says here, the little 3×3 at the bottom right of this data frame.

If we look up here, we're in the last three rows. We want those three rows, but specifically at these last three columns. So this little 3×3 right here is what we're intending to get.

And it should ultimately look something like this. Because the data frame—it will have its column names and row numbers. And then it will have these sets of values for each one.

Fuel Efficiency, Latest Launch, and Power Performance Factor. So your task is to do this twice: once using iloc, and then once using loc.

Get a little practice. All right, I'll see you in the next video. We'll go over the solution.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

But first, give it a try yourself.

Learn more in these courses

How to Learn Machine Learning

Master machine learning with hands-on training. Use Python to make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram