Indexing

What is Indexing in Pandas?

  • Indexing in Pandas means selecting particular rows and columns from a DataFrame

  • Indexing in Pandas is same as we did for a Python List and a NumPy array

  • Pandas has two known operators used for indexing.

  • There are two different methods of indexing in Pandas:

    • loc - label based selection

    • iloc - index based selection

Index Based Selection

  • Index - based selection is to select data based on its numerical position in dataframe.

  • iloc is used for selecting data based on numerical position

  • The syntax for using iloc operator is df.iloc[ ], where df is a dataframe name. You can pass the numerical positions of rows and columns to select in the square bracket.

  • Do you remember the indexing in a NumPy array? If not, don’t worry, you will soon see its implementation on a dataset.

  • Import Pandas Library and load ‘exam_scores.csv’ file

  • Selecting data based on its numerical position. Used operator - iloc

  • Selecting data based on its numerical position. Used operator - iloc

  • Selecting data based on its numerical position. Used operator - iloc

  • You can also pass list of indexes

  • Selecting data based on its numerical position. Used operator - iloc

  • You can also pass negative indexes

Label Based Selection

  • Label based selection selects data based on the column or row names/index. This becomes important while selecting data from a dataframe

  • Label based selection is done with loc

  • Do you remember the python default indexing of the dataframe and the indexes/names that you changed in the previous topics?

  • loc and iloc are conceptually similar. The difference is that iloc considers the default indexing while loc ignores the default indexing

  • loc is used for selecting data based on the data index value/name, not the numerical positions.

  • The syntax for loc is similar to iloc: df.loc[ ], df is a dataframe.

  • We will learn about its implementation on a dataset soon.

  • Selecting data from a dataframe using column and row names/index. Used operator - loc

Last updated

Was this helpful?