| by Arround The Web | No comments

Pandas Get Index Values

Rows’ indexes are known as dataframe indexes, whereas columns’ indexes are known as general column names. Indexes are mostly used to retrieve the data or store the data inside the dataframe. However, we can also retrieve the index itself by using the .index property. In this tutorial, we will teach you how to get an index as a list object, how you can convert the index into a dataframe column to retrieve it, and how you can get the index by using multiple conditions and the index property of pandas.

How to Get Index Values in Pandas?

The DataFrame.index property can be used to obtain a Pandas DataFrame’s index. An Index list object containing the DataFrame’s index is returned by using the DataFrame.index property.

Syntax: pandas.Index(data = None, dtype = None, copy = False, name = None, tupleize_cols = True, **kwargs)

Where:

data: array-like (unidimensional)

dtype: NumPy dtype. It is ‘object’ by default. We will choose the dtype that is suitable for the data if dtype is “None”. If a dtype is specified and safe, dtype will be coerced. If not, a warning will be displayed.

copy: bool. A copy of the given ndarray will be made.

name: object. The name that is being stored in the index.

tupleize_cols: bool. By default, it is True. If True, it will try to create a MultiIndex.

The methods to get the index of the dataframe are demonstrated in the examples below.

Example # 01: Extract the Dataframe Row Index Using the ndex Property

Let’s create a dataframe, first, with multiple rows so we can demonstrate how to get its row index using the pandas index property. Before creating the dataframe, we will import the pandas module to use its functions.

By using a list inside the pd.DataFrame() function, we have created our dataframe. There are two columns in our dataframe: name and age. The column ‘name’ is storing the names of some random persons (‘Tyson’, ‘Jack’, ‘Bruce’, ‘Peter’, ‘Nick’, ‘Haris’, ‘Randy’). Whereas, the column ‘age’ is consisting of the ages of each person (23, 25, 25, 24, 21, 26, 25). At the start of each row, there is an index value for each row created by the pandas constructor by default. Now, we will use the index property to extract this index column.

The output is showing that the rows are starting from 0, incrementing by 1, and ending on the index before 7.

To print each index value, we can use an iterator and inside it or the function print().

Now, we have printed all the values from index 0 to 7.

Example # 02: Extract the Dataframe Row Index Using a Condition

The index values can be retrieved by specifying a condition. The index property will fetch the index values of the dataframe that satisfy the specified condition. Then, we will use the tolist() function to return the fetched values as a list. Using the pd.DataFrame() function, let’s first create our dataframe.

By using a python dictionary inside the pd.Dataframe() function, we have created a dataframe. Our dataframe consists of three columns and 8 rows from 0 to 7. The column ‘items’ is storing the data values as string (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’). The column ‘price’ is containing the numeric values representing the piece of each item (100, 200, 150, 100, 200, 320, 100, 100). The column code having the data values (‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’, ‘u’, ‘’’). Now, let’s retrieve the index values using the script below.

Using the index property, we have retrieved the ‘df’ dataframe’s indexes. Then, we have specified a condition to extract data where the values in the price column are equal to 100. After obtaining the data, we fetched the index values of rows which are satisfying the specified condition. In the end, the tolist() function is used to transform the output into a list object. The function has retrieved a list with four index values, [0, 3, 6, 7].

Example # 03: Extract the Dataframe Column Index Using the get_loc() Function

We have seen how to retrieve the values of a dataframe’s row indexes. However, we can also retrieve the values of a dataframe’s column indexes. To get the index value of any dataframe’s column, the get loc() function can be used. To find the index, we merely supply the column label to the get_loc() function. Let’s create a dataframe consisting of more than one column so we can retrieve its index location or index value.

In our dataframe, we have created four columns: class, members, salary, and expense. The class column is storing the data values (‘a’, ‘a’, ‘b’, ‘b’, ‘c’, ‘c’, ‘d’, ‘d’). The column members contains values (5, 4, 5, 5, 4, 6, 4, 4), while the column salary and expense contains the numerical data values (30000, 28000, 32000, 31000, 30000, 26000, 27000, 28000) and (12000, 11000, 11500, 13000, 10000, 12500, 10500, 13000), respectively. Suppose we have to find the index value of column salary:

The function has retrieved the index of the specified column, 2.

Example # 04: Extract the Specified Row Index Values Using the get_loc() Function

We can also retrieve the index location of row indexes using the get_loc() function if the labels for row indexes are specified. We can specify the labels to our row index by using a list containing names for each value of the row index. Let’s add the index labels to the dataframe which we have created in example # 3.

Inside the pd.DataFrame() function, we have specified the index parameter as a list containing labels from r1 to r8. The labels ‘r1′,’r2′,’r3′,’r4’, ‘r5’, ‘r6’, ‘r7’, and ‘r8’ has replaced the by default integer index of dataframe. Now, let’s retrieve the location of the index for a specific label.

First, the index property is applied to the dataframe to obtain the indexes. Then, get_loc() function is applied to extract the index position of the specified index label of the row.

Example # 05: Extract the Row Index Values Using the Numpy Where() Function:

We can also get the index values by specifying a condition inside the where() function of numpy. Let’s create a dataframe first, we will import the pandas as well as the NumPy library to use its functions.

After importing the required libraries, we have created our dataframe. In our dataframe, we have three columns (id, price, and discount). The columns id, price, and discount stores the data values (‘001’, ‘002’, ‘003’, ‘004’, ‘005’, ‘006’, ‘007’, ‘008’), (100, 150, 130, 200, 120, 170, 120, 140), and (30, 40, 10, 20, 60, 10, 30, 60), respectively. Now, let’s find the row index value using the where() function inside the list() function.

We have specified a condition inside the where() function to get the rows where the value in the column ‘discount’ is greater than 30. To create a list out of the returned values, we have used the list() method.

Conclusion

This article covered how to retrieve dataframe index values in Pandas. We used different functions to retrieve the row and column index of the dataframe. We implemented multiple examples to teach you how to extract the dataframe row index using the index property, using conditions, and using the get_loc() function. Also, we have discussed how to get column index values using the get_loc() function.

Share Button

Source: linuxhint.com

Leave a Reply