Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 1    ❮    Lesson List    ❮    Top Page

1.1  DataFrame

1.2  Data Types Conversion

❯  1.3  Rows Selection

1.4  Columns Selection

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   6m 27s
☷  Interactive readings   5m

Indexing using Integer with .iloc 

Let's read in the college dataset with the institution name as the index, and select a single column as a Series with the indexing operator.

The .iloc indexer makes selections only by integer location. Passing an integer to it returns a scalar value.

To select several different integer locations, pass a list to .iloc. This returns a Series.

To select an equally spaced partition of data, use slice notation.

> When passing a scalar value to the indexing operator, a scalar value is returned. When passing a list or slice, as in the other steps, a Series is returned. To select a single item and retain the item in its Series, pass in as a single-item list rather than a scalar value: city.iloc[[3]]

Indexing using Name with .loc 

Unlike .iloc, the method .loc selects only with index's name or labels.

  • Passing a single string returns a scalar value.

  • To select several disjoint labels, use a list.

  • To select an equally spaced partition, use slice notation. Make sure that the start and stop values are strings. You can use an integer to specify the step size of the slice.

Checking the difference of .iloc vs .loc

Here, we will see how to do the same thing with the two indexers, .iloc and .loc. Notice the difference.

Selecting Disjoint Rows

To select a disjointed set of rows as a DataFrame, pass a list of integers to the .iloc indexer.

The same DataFrame may be reproduced using .iloc by passing it a list of the exact institution names.

> The list of index labels can be selected directly from the DataFrame returned without the need for copying and pasting:
college.iloc[[60,99,3]].index.tolist()

Selecting Rows using Slice

The slice notation can be used with .iloc to select an entire segment of the data.

Slice notation also works with the .loc indexer and is inclusive of the last label.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse