Finding the index of the first row in a Pandas DataFrame, starting from a specific index and moving backwards, until condition is met

huangapple go评论71阅读模式
英文:

Finding the index of the first row in a Pandas DataFrame, starting from a specific index and moving backwards, until condition is met

问题

我有一个 Pandas DataFrame,我正在尝试找到两列值不同的第一行的索引。但是,我需要以相反的顺序开始搜索。

这是一个示例:

import pandas as pd

data = {'Column1': [1, 2, 3, 5, 5],
        'Column2': [1, 1, 3, 5, 5]}

df = pd.DataFrame(data)

在这里,例如,我想从倒数第二个索引开始,直到找到第一个列值不同的索引。因此,期望的输出是 "1"。

英文:

I have a Pandas DataFrame and I'm trying to find the index of the first row where two column values differ. However, I need to start searching in reverse order.

Here's an example:

import pandas as pd

data = {'Column1': [1, 2, 3, 5, 5],
        'Column2': [1, 1, 3, 5, 5]}

df = pd.DataFrame(data)

Here, for example, I want to start from the second last index until I find the first index, where the column values differ. Thus, the desired Output would be "1".

答案1

得分: 1

使用索引过滤行,例如按索引的 i 值,通过 Series.ne 进行不等值比较,然后使用 boolean indexing 以及 Series.last_valid_index 来获取结果:

注意:如果没有差异,则输出为 None

i = 3
s = df.loc[:i, 'Column1'].ne(df.loc[:i, 'Column2'])  

out = s
展开收缩
.last_valid_index()

另一个方法是使用 nextiter

i = 3
s = df.loc[:i, 'Column1'].ne(df.loc[:i, 'Column2'])  

out = next(iter(s.index
展开收缩
[::-1]), None)
英文:

Filter rows by indexing, e.g. to i value of index, comapre for not equal by Series.ne and last use boolean indexing with Series.last_valid_index:

Notice: If no difference output is None.

i = 3
s = df.loc[:i, 'Column1'].ne(df.loc[:i, 'Column2'])  

out = s
展开收缩
.last_valid_index()

Another idea with next and iter:

i = 3
s = df.loc[:i, 'Column1'].ne(df.loc[:i, 'Column2'])  

out = next(iter(s.index
展开收缩
[::-1]), None)

huangapple
  • 本文由 发表于 2023年7月6日 17:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定