df = pd.DataFrame({'A':['A','B','C','D'],
'B':[4,5,6,7]})
A B
A 4
B 5
C 6
D 7
我想返回一种方法来返回从给定字符串开始的所有行,在A列中说“B”。
A B
B 5
C 6
D 7
去Deacs!
答案 0 :(得分:2)
如果字符串始终存在,则可以将o.a.s.ml.linalg.VectorUDT
与条件Series一起使用以查找字符串首次出现的索引,然后使用idxmax()
方法在索引后提取行:< / p>
tail()
另一个可能更安全的方法,即使该列中不存在该字符串仍然有效:
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
答案 1 :(得分:0)
假设A列中的数据按字母顺序排序,您可以使用子集,类似
df[df['A'] >= 'B']
会做到这一点。
答案 2 :(得分:0)
如果列A
未按字母顺序排序,则可以使用此解决方案。
此外,如果列B
包含多个值A
,这将从列A
中第一次出现B
的行开始数据框。
idx = df[df['A'] == 'B'].index[0]
df = df[idx:]
print(df)
A B
1 B 5
2 C 6
3 D 7
答案 3 :(得分:0)
一个很好地概括的答案可以使用numpy.argwhere
idx = np.argwhere(df.A == 'B')[0][0]
df.iloc[idx:]