Knowledge Base

4. Slicing and Indexing

4.1. List

Index a list: Use square brackets and index numbers to access data in a list. If indexing from the beginning of the list, the indices are 0, 1, 2, etc. If indexing from the end of the list, the indices are -1, -2, -3, etc.

Slice a list: Use square brackets and put a colon between the starting index and the last index +1. Ex. list[2:5] returns elements 2, 3, and 4. To include all elements before/after an index, drop the start/end index. Ex. list[2:] returns all elements from index 2 to the end and list[:100] returns elements 0 to 99.

4.2. Nested List

Index a nested list: Use a square bracket and index number to index a nested list followed by a second square bracket and index number to index an element in the nested list. Ex. list[2][3] accesses the 3rd sublist (in index 2) and returns the 4th element (in index 3). Note: the element in index 3 could be another list. To access, for example, the 1st element in this list, use list[2][3][0].

Slice a nested list: Follow the same logic as slicing a list. Ex. list[2][0:3] accesses the 3rd sublist (in index 2) and returns the 1st through the 3rd items in the sublist (in indices 0 to 2).

4.3. Dictionaries

Index a dictionary: Use square brackets with a key in quotes to access elements in a dictionary. Ex. dict['Weather'] returns the value of the key-value pair whose key is ‘Weather’. Note: if the specified key does not exist, a keyerror will occur. To avoid errors, use the dictionary object’s get() method (see section 3.3.)

4.4. pandas DataFrame

Selecting columns: Place the column name in quotes inside square brackets. Ex. df['Miles'] returns the column Miles of the DataFrame object df as a pandas Series object. You may also provide a list of column names. Ex. df[[’Miles’,’Town’]] returns the columns Miles and Towns in df.

Indexing/Slicing a column: Select a column and then follow the syntax as if you were indexing/slicing a list (see section 4.1.). Ex. df[’Town’][:5] returns the first 4 items in column Town.

The pandas DataFrame loc[ ] property allows you to access a group of rows by index values or conditional statements.

  • Accessing rows/columns by index value

    Specify the range of index values inside loc[]. Ex. df.loc[:4] returns all columns and rows corresponding to indices 0 to 4. df.loc[:4,’Miles’] returns rows corresponding to indices 0 to 4 of the Miles column.

  • Accessing rows/columns by a conditional statement:

    Place a conditional statement inside loc[] using columns of df. Ex. df.loc[df[’Miles’]>100] returns all rows where column Miles is greater than 100. df.loc[df[’Miles’]>100][’Town’] returns Town values where column Miles is greater than 100.

Use the interactive below to get a more intuitive understanding of .loc[].

Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,