Pandas is one of the most popular data manipulation libraries in Python. It provides powerful tools for data analysis, including the .apply() function, which allows users to apply a function to each row or column of a DataFrame. In some cases, you may need to access the index of the row while applying a function.
In this blog, we will learn how to get the index of a row in a Pandas apply function.
Table of Contents
Understanding the Pandas .apply() Function
The .apply() function is used to apply a custom function to rows or columns of a Pandas DataFrame.
- When used with axis=1, the function is applied row-wise (to each row).
- When used with axis=0, the function is applied column-wise (to each column).
Example: Accessing the Index of a row inside Pandas apply() function
We can access the index of the row using the .name attribute of the row.
Code:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['a', 'b', 'c'])
def rowFunc(row):
return row.name
df['Row Index'] = df.apply(rowFunc, axis=1)
print(df)
Output:
Explanation:
- row.name gives the index of the row.
- We have created a new column ‘d’ and assigned the index of the row to them.
Alternative Approach Using Lambda Function
A lambda function is an anonymous function in Python that helps in writing concise, one-liner operations. In Pandas, we can use lambda with functions like apply() to perform row-wise or column-wise transformations.
Syntax:
df['New_Column'] = df.apply(lambda row: some_operation(row), axis=1)
df['New_Column'] = df.apply(lambda col: some_operation(col), axis=0)
Explanation:
- axis=1 is used for row-wise traversal
- axis=0 is used for column-wise traversal
Example to get the index of a row using Lambda Function
Here is an example to get the index of a row using the lambda function:
Code:
import pandas as pd
df = pd.DataFrame({
'A': [10, 20, 30],
'B': [5, 15, 25]
})
df['Row_Index'] = df.apply(lambda row: row.name, axis=1)
print(df)
Output:
Explanation: Here, we have added a new column Row_index, and stored the index of the row in that column.
Conclusion
The apply() function is used to access the row and column of the dataframe. With the help of the .name attribute, we can get the index of a row. So far in this article, we have learned how we can get the index of a row in a Pandas apply function.
If you want to build a career in data science, then you should check out our Data Science Course to become an industry expert.
FAQs
1. How do you print row indexes in Pandas?
To print the row index of a dataframe, you can use the df.index attribute in Pandas.
2. How does index () work?
The index() method in Python returns the position of the first occurrence of a specified value in a list or tuple.
3. What does set_index() do?
The set_index() method in Pandas sets one or more columns as the index of a DataFrame, in order to modify the row labels.
4. How to use count() in Python?
The count() method is used to count the frequency of a value in a list/string.
5. How to apply a function to all rows in Pandas?
We can use df.apply(function, axis=1) to apply a function row-wise in a Pandas DataFrame.