- Pandas Get Index from DataFrame?
- 1. Quick Examples of getting Index from Pandas DataFrame
- 2. Get Index from Pandas DataFrame
- 3. Get Pandas Index as a List
- 4. Get Pandas Index using tolist()
- 5. Get Column Index Using the get_loc()
- 6. Get Row Index Using the Numpy Where() Function:
- 7. Conclusion
- Related Articles
- References
- You may also like reading:
- How to Get the Index of a Dataframe in Python Pandas?
- Methods to Get Index of a Dataframe in Python
- Method 1: Using for loop
- Method 2: Using index attribute
- Method 3: Using index.values property
- Method 4: Using tolist() function
- Method 5: Using query() and tolist() functions
- Summing-up
- How to Get Index of Pandas DataFrame?
- Syntax
- Examples
- 1. Get index of given DataFrame df
- 2. Iterate over index and print
- 3. Get index of DataFrame that has been initialised with specific index
- Summary
Pandas Get Index from DataFrame?
How to get an index from Pandas DataFrame? DataFrame.index property is used to get the index from the DataFrame. Pandas Index is an immutable sequence used for indexing DataFrame and Series. The DataFrame index is also referred to as the row index, by default index is created on DataFrame as a sequence number that starts from 0 and increments by 1. You can also assign custom values to Index.
Using the index we can select the rows from the given DataFrame or add the row at a specified Index. we can also get the index itself of the given DataFrame by using the .index property . In this article, I will explain the index property and using this property how we can get an index of DataFrame and also explain how to get the index as a list object using index.values.
1. Quick Examples of getting Index from Pandas DataFrame
If you are in a hurry, below are some quick examples of how to get an index from DataFrame.
# Below are the quick examples # Example 1: Get the index use df.index property print(df.index) # Example 2: Get the index use index.values print(list(df.index.values)) # Example 3: Get the index use tolist() print(list(df.index.values.tolist())) # Example 4: Get the column index use get_loc() print(df.columns.get_loc('Fee')) # Example 5: Get the index values using np.where() print(list(np.where(df["Discount"] > 1200)))
2. Get Index from Pandas DataFrame
You can get the Index from the pandas DataFrame by using .index property, this index property returns Series object. Let’s create DataFrame using data from the Python dictionary then call the index property on DataFrame to get the index. When we call index property with no specified index, it will return the complete index.
# Create DataFrame import pandas as pd technologies = < 'Courses':["Spark","PySpark","Python","pandas"], 'Fee' :[20000,25000,22000,30000], 'Duration':['30days','40days','35days','50days'], 'Discount':[1000,2300,1200,2000] >df = pd.DataFrame(technologies) print(df) # Get the index of DataFrame print(df.index)
# Output: Courses Fee Duration Discount 0 Spark 20000 30days 1000 1 PySpark 25000 40days 2300 2 Python 22000 35days 1200 3 pandas 30000 50days 2000 RangeIndex(start=0, stop=4, step=1)
By default it returns the type of Index, since we have a range index it returned RangeIndex(). Using any looping (Python for loop) technique we can access individual indexes of a given DataFrame in Python.
# Get the index of Dataframe use for loop for i in df.index: print(i) # Output: # 0 # 1 # 2 # 3
3. Get Pandas Index as a List
Sometimes you may be required to get the pandas DataFrame index as a list, we can do this by using df.index.values . Let’s pass tthis into a list, it will return the index as a list.
# Get the index use index.values print(list(df.index.values)) # Output: # [0, 1, 2, 3]
4. Get Pandas Index using tolist()
Alternatively using the Pandas tolist() function we can return the index of DataFrame as a list. For example,
# Get the index as List using tolist() print(df.index.values.tolist()) # Output: # [0, 1, 2, 3]
5. Get Column Index Using the get_loc()
From the above, we came to know how to retrieve the row index of DataFrame. However, we can also get the index of DataFrame column using the get_loc() function. For that, we have to pass the column label which we want to get its index to the get_loc() function. It will return the index location.
# Get the column index use get_loc() print(df.columns.get_loc('Fee')) # Output: # 1
6. Get Row Index Using the Numpy Where() Function:
We can also get the index by specifying a condition passed into numpy.where() function. Let’s use NumPy library to use its functions.
# Get the index values using np.where() print(list(np.where(df["Discount"] > 1200))) # Output: # [array([1, 3], dtype=int64)]
7. Conclusion
In this article, I have explained how to get the index of Pandas DataFrame by using the .index property , index.values , tolist(), function, and NumPy where() function with well-defined examples.
Related Articles
References
You may also like reading:
How to Get the Index of a Dataframe in Python Pandas?
Hello folks! In this tutorial, we are going to discuss the different ways to get the index or rows of a pandas DataFrame object. So, let’s get started.
Methods to Get Index of a Dataframe in Python
Let’s get right into the steps to find the index of a dataframe. Also, check out how you can reset the index of a dataframe to ensure that everytime you append, or sort a dataframe, the index numbers are aligned.
Method 1: Using for loop
In Python, we can easily get the index or rows of a pandas DataFrame object using a for loop. In this method, we will create a pandas DataFrame object from a Python dictionary using the pd.DataFrame() function of pandas module in Python. Then we will run a for loop over the pandas DataFrame index object to print the index. Let’s implement this through Python code.
# Method-1 # Import pandas import pandas as pd # Create a Python dictionary data = # Create a DataFrame object from above dictionary df = pd.DataFrame(data, index = [1, 2, 3, 4, 5]) print("This is DataFrame:\n") print(df) # Get the index/rows of the above DataFrame # Using for loop iteration print("\nThis is index of DataFrame:\n") for idx in df.index: print(idx, end = ' ')
This is DataFrame: Name Roll 1 Sanjay 101 2 Shreya 102 3 Raju 103 4 Gopal 104 5 Ravi 105 This is index of DataFrame: 1 2 3 4 5
Method 2: Using index attribute
This is the most widely used method to get the index of a DataFrame object. In this method, we will be creating a pandas DataFrame object using the pd.DataFrame() function of as usual. Then we will use the index attribute of pandas DataFrame class to get the index of the pandas DataFrame object. As we apply the index attribute on the pandas DataFrame object, it returns a tuple that contains the index list of the DataFrame. Let’s see how we can actually implement this in Python programming.
# Method-2 # Import pandas import pandas as pd # Create a Python dictionary data = # Create a DataFrame object from above dictionary df = pd.DataFrame(data, index = ['s1', 's2', 's3', 's4', 's5']) print("This is DataFrame:\n") print(df) # Get the index/rows of the above DataFrame # Using index attribute print("\nThis is index of DataFrame:\n") index_list = df.index print(index_list)
This is DataFrame: Name Roll CGPA s1 Sanjay 101 8.15 s2 Shreya 102 8.18 s3 Raju 103 9.32 s4 Gopal 104 8.85 s5 Ravi 105 7.87 This is index of DataFrame: Index(['s1', 's2', 's3', 's4', 's5'], dtype='object')
Method 3: Using index.values property
First, we will create a pandas DataFrame object using the pd.DataFrame() function of pandas Python module. Then we will use the index.values property of pandas DataFrame object to access its index list. As we apply the index.values property on the pandas DataFrame object, it returns an array that represents the data in the index list of the pandas DataFrame object. Let’s get into the Python code to implement this method of getting the DataFrame’s index list.
# Method-3 # Import pandas import pandas as pd # Create a Python dictionary data = # Create a DataFrame object from above dictionary df = pd.DataFrame(data) print("This is DataFrame:\n") print(df) # Get the index/rows of the above DataFrame # Using index.values property print("\nThis is index of DataFrame:\n") index_list = df.index.values print(index_list)
This is DataFrame: Name Roll Branch CGPA 0 Sanjay 101 ECE 8.15 1 Shreya 102 CSE 8.18 2 Raju 103 EEE 9.32 3 Gopal 104 ICE 8.85 4 Ravi 105 IPE 7.87 This is index of DataFrame: [0 1 2 3 4]
Method 4: Using tolist() function
This is a handy tool of the pandas module which converts the index of a pandas DataFrame object into a Python list. In this method, we create a pandas DataFrame object using the pd.DataFrame() function as we did in the previous methods. Then we will access the pandas DataFrame index object using the index attribute of the pandas DataFrame class. Finally, we will apply the tolist() function which actually returns the index of the DataFrame in the form of a Python list. Let’s write the Python program to implement this handy method to get the index of a pandas DataFrame in a Python list.
# Method-4 # Import pandas import pandas as pd # Create a Python dictionary data = # Create a DataFrame object from above dictionary df = pd.DataFrame(data, index = ['R1', 'R2', 'R3', 'R4', 'R5']) print("This is DataFrame:\n") print(df) # Get the index/rows of the above DataFrame # Using tolist() function print("\nThis is index of DataFrame:\n") index_list = df.index.tolist() print(index_list)
This is DataFrame: Name Roll Branch CGPA R1 Sanjay 101 ECE 8.15 R2 Shreya 102 CSE 8.18 R3 Raju 103 EEE 9.32 R4 Gopal 104 ICE 8.85 R5 Ravi 105 IPE 7.87 This is index of DataFrame: ['R1', 'R2', 'R3', 'R4', 'R5']
Method 5: Using query() and tolist() functions
Using this method, we can get only the specific indices of the pandas DataFrame object satisfying certain criteria. In this method, we will create a pandas DataFrame object using the pd.DataFrame() function and use the query() function of the pandas DataFrame class. When we apply the query() function on the DataFrame and pass a condition, it returns a DataFrame which contains only the rows which satisfy the condition passed to it.
After this we will apply the index attribute of the DataFrame class and use the tolist() function which returns a Python list of the DataFrame index values.
Let’s see the Python code to implement this useful method to get the selected rows or indices of the pandas DataFrame object satisfying the given conditions.
# Method-5 # Import pandas import pandas as pd # Create a Python dictionary data = # Create a DataFrame object from above dictionary df = pd.DataFrame(data, index = ['I', 'II', 'III', 'IV', 'V']) print("This is DataFrame:\n") print(df) # Get the index/rows of the above DataFrame # Using query() and tolist() functions print("\nThis is index of DataFrame:\n") index_list = df.query("CGPA > 8.5").index.tolist() print(index_list)
This is DataFrame: Name Roll Branch CGPA I Sanjay 101 ECE 8.15 II Shreya 102 CSE 9.32 III Raju 103 EEE 8.78 IV Gopal 104 ICE 7.87 V Ravi 105 IPE 8.85 This is index of DataFrame: ['II', 'III', 'V']
Summing-up
In this tutorial, we have learned the four different methods to get the index of the DataFrame object. Hope you have understood the above and are excited to experiment with these methods on your own. Thank you and stay tuned with us for more such kinds of Python tutorials.
How to Get Index of Pandas DataFrame?
To get the index of a Pandas DataFrame, call DataFrame.index property. The DataFrame.index property returns an Index object representing the index of this DataFrame.
Syntax
The syntax to use index property of a DataFrame is
The index property returns an object of type Index. We could access individual index using any looping technique in Python.
Examples
1. Get index of given DataFrame df
In the following program, we have a DataFrame with no index specified. Since no index is specified, a range that starts at 0 and increments in steps of 1 would be assigned to the DataFrame. Let us get the index of this DataFrame using DataFrame.index.
Python Program
import pandas as pd df = pd.DataFrame( [[88, 72, 67], [23, 78, 62], [55, 54, 76]], columns=['a', 'b', 'c']) index = df.index print(index)
RangeIndex(start=0, stop=3, step=1)
2. Iterate over index and print
We can print the elements of Index object using a for loop as shown in the following.
Python Program
import pandas as pd df = pd.DataFrame( [[88, 72, 67], [23, 78, 62], [55, 54, 76]], columns=['a', 'b', 'c']) index = df.index for i in index: print(i)
3. Get index of DataFrame that has been initialised with specific index
Let us consider another example, where we have set a specific index for a DataFrame. And we are going to get the index of this DataFrame using DataFrame.index.
Python Program
import pandas as pd df = pd.DataFrame( [[88, 72, 67], [23, 78, 62], [55, 54, 76]], index=[2, 4, 9], columns=['a', 'b', 'c']) index = df.index print(index)
Int64Index([2, 4, 9], dtype='int64')
Summary
In this tutorial of Python Examples, we learned how to get the index of a DataFrame using DataFrame.index property.