df.tail(-(df.A == "B").idxmax()) # this method works if the string exists in the column
# and the index of the data frame is a normal sequence as given by range(n)
# A B
#1 B 5
#2 C 6
#3 D 7
另一种可能更安全的方法,即使列中不存在字符串,也仍然有效:
df[(df.A == "B").cumsum().astype(bool)]
# A B
#1 B 5
#2 C 6
#3 D 7
如果字符串始终存在,可以使用
idxmax()
和条件序列来查找字符串第一次出现的索引,然后使用tail()
方法提取索引后的行:另一种可能更安全的方法,即使列中不存在字符串,也仍然有效:
假设A列中的数据是按字母顺序排序的,您可以使用子集,这类似于
会成功的。你知道吗
一个概括得很好的答案可以使用
numpy.argwhere
相关问题 更多 >
编程相关推荐