getting the index of a row in a pandas apply function
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When you use DataFrame.apply(..., axis=1) in pandas, each row is passed to your function as a Series. The row index is already there: you access it with row.name.
That is the simplest answer, but it is also a good reminder that apply is row-wise Python code, not a special vectorized engine. If you only need the index for a transformation, row.name is convenient. If you need performance on large data, it is often worth looking for a vectorized alternative.
The Basic Pattern
Here is the direct way to access the row index.
row.name contains the index label for that row.
What row.name Actually Is
Inside a row-wise apply, row is a Series representing one row. Its .name attribute is the row index label.
If your index is numeric, row.name may look like an integer. If your index is a string or datetime index, row.name is that label type instead.
That means the technique works across normal index types without special handling.
Example With a Numeric Index
If the index is 0, 1, 2, then row.name gives those positions.
MultiIndex Case
If the DataFrame uses a MultiIndex, row.name becomes a tuple.
That is often cleaner than resetting the index just to get at the labels.
When apply Is Not the Best Tool
apply(axis=1) is easy to read, but it is relatively slow because it executes Python code row by row.
If you only need the index values for a vectorized computation, consider:
- '
df.index' - '
df.reset_index()' - direct column operations instead of row-wise
apply
For example, if you want the index as a column, this is often simpler:
Now the former index is a regular column and can participate in vectorized expressions.
Why row.name Is Still Useful
Despite the performance caveat, row.name is very handy when the row logic is genuinely custom and the index is part of the business rule.
Examples include:
- generating row-specific labels
- comparing index values with row content
- building per-row lookup keys
In those situations, it is the idiomatic answer.
Common Pitfalls
A common mistake is expecting a separate “row index argument” to be passed automatically into the function. In pandas, the row label is already available on the row Series as name.
Another mistake is assuming row.name is always an integer position. It is the index label, which may be a string, timestamp, or tuple.
Developers also overuse apply(axis=1) for operations that could be vectorized much faster.
Finally, after reset_index(), the old index becomes ordinary data. If you change the DataFrame shape afterward, make sure you know whether you still want the original labels or the new default index.
Summary
- Inside
DataFrame.apply(..., axis=1), the row index is available asrow.name. - '
row.namereturns the index label, not necessarily a numeric position.' - For
MultiIndex,row.nameis typically a tuple. - Use
applyfor custom row logic, but prefer vectorized alternatives when performance matters. - If you want the index as ordinary data,
reset_index()is often a cleaner design.
Related reading
- Ghost line in Tensorboard scalar plot
- GLM Warning message 'newdata' had 16623 rows but variables found have 22488 rows
- Google Colaboratory local runtime using local GPU
- gradient descent using python and numpy
- Getting the index of the returned max or min item using max/min on a list
- Getting the SQL from a Django QuerySet
- Getting the name of a variable as a string
- Getting today's date in YYYY-MM-DD in Python?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.