In a Pandas DataFrame, the rows or index names represent the labels assigned to each row. It can be useful to extract or work with these row names for various data manipulation tasks. In this article, we will explore different methods to obtain the rows/index names in a Pandas DataFrame.
Method 1: Using the .index
Attribute
The first method involves using the .index
attribute of a DataFrame to retrieve the index object, which represents the row/index names. Here’s an example:
import pandas as pd data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # Get the index object row_names = df.index print(row_names)
RangeIndex(start=0, stop=3, step=1)
In the above code snippet, we create a DataFrame with two columns: ‘Name’ and ‘Age’. By accessing the .index
attribute of the DataFrame, we obtain the index object, which represents the row names. In this case, the index is a RangeIndex starting from 0 and ending at 3 (exclusive).
Method 2: Using the .iterrows()
Method
The second method involves using the .iterrows()
method, which allows us to iterate over the rows of a DataFrame along with their corresponding index names. Here’s an example:
import pandas as pd data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # Iterate over the rows and print the index names for index, row in df.iterrows(): print(index)
0 1 2
In this code snippet, we use the .iterrows()
method to iterate over the rows of the DataFrame. For each row, we extract the index name using the index
variable and print it.
Method 3: Using the .loc[]
or .iloc[]
Indexing
The third method involves using the .loc[]
or .iloc[]
indexing methods to access specific rows of the DataFrame. By specifying the row index, we can retrieve the corresponding row name. Here’s an example:
import pandas as pd data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # Get the row name using .loc[] row_name = df.loc[1].name print(row_name) # Get the row name using .iloc[] row_name = df.iloc[2].name print(row_name)
1 2
In this code snippet, we use the .loc[]
method to access the row with index 1 and retrieve its corresponding name using the name
attribute. Similarly, we use the .iloc[]
method to access the row with index 2 and retrieve its name.
Method 4: Using the .keys()
Method
The .keys()
method returns the column labels as a list. We can use this method in combination with the .index
attribute to obtain the row/index names. Here’s an example:
import pandas as pd Create a sample DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) Get the row names using .keys() and .index row_names = list(df.keys())[df.index] print(row_names)
[0, 1, 2]
In this code snippet, we first use the .keys()
method to retrieve the column labels as a list. We then use the .index
attribute to obtain the row indices. Finally, we create a new list by indexing the column label list with the row indices, giving us the row names.
Method 5: Using the .index.values
Attribute
The .index.values
attribute returns the row/index names as a NumPy array. We can convert this array to a list to work with the row names. Here’s an example:
import pandas as pd Create a sample DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) Get the row names using .index.values row_names = list(df.index.values) print(row_names)
[0, 1, 2]
In this code snippet, we directly access the .index.values
attribute, which returns the row/index names as a NumPy array. We convert this array to a list using the list()
function to obtain the row names.
Method 6: Using the .reset_index()
Method
The .reset_index()
method is used to reset the index of a DataFrame, creating a new column named ‘index’ containing the row names. Here’s an example:
import pandas as pd Create a sample DataFrame data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) Reset the index and get the row names df_with_names = df.reset_index() row_names = df_with_names['index'] print(row_names)
0 0 1 1 2 2 Name: index, dtype: int64
In this code snippet, we use the .reset_index()
method to reset the index of the DataFrame. This creates a new column named ‘index’ containing the row names. We then extract the ‘index’ column to obtain the row names.
Last Updated on May 17, 2023 by admin