2

This question is related to but different from this one, which wonders how to access the row index from within apply. That can be done with row.name. However, in my case I am applying a function to query'd dataframe, and the row's name are just their index in the original df. I need them to be zero-based for the queried DataFrame.

import pandas as pd


def print_name(r):
    print(r.name)

data = {"seg": [1, 1, 1, 2, 2, 2], "text": ["i", "like", "you", "do", "you", "see"]}
df = pd.DataFrame(data=data)
sub = df.query("seg==2")
sub.apply(print_name, axis=1)
# 3
# 4
# 5
# Expected 0, 1, 2
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239

1 Answers1

0

I think you want default index, because filtered rows has original index, there is no change:

sub = df.query("seg==2").reset_index(drop=True)

print (df.query("seg==2"))
   seg text
3    2   do
4    2  you
5    2  see

print (df.query("seg==2").reset_index(drop=True))
   seg text
0    2   do
1    2  you
2    2  see
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Exactly. Was just going to answer my own question. I thought that dropping the index for the queried DataFrame would also reset it in the original DataFrame, but that is not the case - which is good. So this answers my question. – Bram Vanroy Jan 12 '21 at 09:18