英文:
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()
另一个方法是使用 next
和 iter
:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论