Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get rows/index names in Pandas dataframe?
Pandas DataFrames have index labels (row names) that identify each row. Getting these row names is essential for data filtering, joining, and analysis operations. Python provides several methods to access DataFrame row names.
Using the index Attribute
The index attribute returns the row names as a Pandas Index object ?
import pandas as pd
# Create a DataFrame with custom row names
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}, index=['X', 'Y', 'Z'])
# Get row names as Index object
row_names = df.index
print("Index object:")
print(row_names)
# Convert to list
row_names_list = df.index.tolist()
print("\nAs list:")
print(row_names_list)
Index object: Index(['X', 'Y', 'Z'], dtype='object') As list: ['X', 'Y', 'Z']
Using index.values
The index.values returns a NumPy array of row names ?
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}, index=['row1', 'row2', 'row3'])
# Get row names as NumPy array, then convert to list
row_names = df.index.values.tolist()
print(row_names)
['row1', 'row2', 'row3']
Using axes Attribute
The axes attribute returns both row and column axes. Index 0 gives row names ?
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}, index=['row1', 'row2', 'row3'])
# Get row names using axes[0]
row_names = df.axes[0].tolist()
print(row_names)
['row1', 'row2', 'row3']
Iterating Through Row Names
Use a for loop to process each row name individually ?
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
}, index=['student1', 'student2', 'student3'])
# Iterate through row names
print("Row names:")
for row_name in df.index:
print(row_name)
Row names: student1 student2 student3
Practical Example
Calculate average test scores for each student using row names ?
import pandas as pd
# Create DataFrame with test scores
df = pd.DataFrame({
'test1': [85, 90, 95],
'test2': [80, 85, 90],
'test3': [75, 80, 85]
}, index=['Alice', 'Bob', 'Charlie'])
# Get student names and calculate averages
students = df.index.tolist()
print("Average scores:")
for student in students:
avg_score = df.loc[student].mean()
print(f"{student}: {avg_score:.1f}")
Average scores: Alice: 80.0 Bob: 85.0 Charlie: 90.0
Comparison
| Method | Returns | Best For |
|---|---|---|
df.index |
Index object | Most operations, keeps metadata |
df.index.values |
NumPy array | When you need array operations |
df.axes[0] |
Index object | When accessing both axes |
df.index.tolist() |
Python list | Standard Python operations |
Conclusion
Use df.index for most row name operations as it preserves metadata. Convert to list with tolist() when you need standard Python list functionality. The axes method is useful when working with both row and column labels.
