VOOZH about

URL: https://www.geeksforgeeks.org/python/label-based-indexing-to-the-pandas-dataframe/

⇱ Label-based indexing to the Pandas DataFrame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Label-based indexing to the Pandas DataFrame

Last Updated : 25 Oct, 2020

Indexing plays an important role in data frames. Sometimes we need to give a label-based "fancy indexing" to the Pandas Data frame. For this, we have a function in pandas known as pandas.DataFrame.lookup(). The concept of Fancy Indexing is simple which means, we have to pass an array of indices to access multiple array elements at once. 

pandas.DataFrame.lookup() function takes equal-length arrays of row and column labels as its attributes and returns an array of the values corresponding to each (row, col) pair.

Syntax: DataFrame.lookup(row_labels, col_labels) Parameters: row_labels - The row labels to use for lookup. col_labels - The column labels to use for lookup. Returns: numpy.ndarray

Example 1:

Output:

👁 Image

Output:

👁 Image

In the above example, we use the concept of label based Fancy Indexing to access multiple elements of data frame at once and hence create a new column 'Value' using function dataframe.lookup()

Example 2:

Output:

👁 Image

Output:

👁 Image

In the above example, we use the concept of label based Fancy Indexing to access multiple elements of data frame at once and hence create two new columns 'Age' and 'Marks' using function dataframe.lookup()

Example 3:

Output:

👁 Image

Output:

👁 Image

In the above example, we use the concept of label based Fancy Indexing to access multiple elements of the data frame at once and hence create two new columns 'Age', 'Height' and 'Date_of_Birth' using function dataframe.lookup()

All three examples show how fancy indexing works and how we can create new columns using fancy indexing along with the dataframe.lookup() function.

Comment