使用 pandas 应用获取子 DataFrame 中行的索引

Get index of row within a sub-DataFrame with pandas apply

这个问题与 this one 相关但不同,它想知道如何从 apply 中访问行索引。这可以用 row.name 来完成。但是,在我的例子中,我将一个函数应用于 query 的数据框,并且该行的 name 只是它们在 original df 中的索引。对于查询的 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

我想你需要默认索引,因为过滤后的行有原始索引,没有变化:

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