Python 大熊猫体内grep-C的当量

Python 大熊猫体内grep-C的当量,python,pandas,Python,Pandas,熊猫中有没有任何功能(或模拟它的方法)与 cat文件名| grep-C 10模式>另一个文件 除了编写循环遍历整个文件的代码之外,它还能在unix上运行吗?解决方案 这是grep-c2“a”的一个类似物 使用grep-C检查 { temp } » grep -n -C 2 'a' aaa.txt /d/temp 16-e 17-s 18:a 19-n

熊猫中有没有任何功能(或模拟它的方法)与

cat文件名| grep-C 10模式>另一个文件

除了编写循环遍历整个文件的代码之外,它还能在unix上运行吗?

解决方案
这是grep-c2“a”的一个类似物

使用grep-C检查

{ temp }  » grep -n -C 2 'a' aaa.txt                                                                          /d/temp
16-e
17-s
18:a
19-n
20-h
21-w
22-q
23:a
24-i
25:a
26-s
27-l
注意:
grep
统计从
1
开始的行,pandas从
0
开始-这就是为什么与pandas索引相比
grep
的行号总是在
前面一行

设置:

import string
data = list(string.ascii_lowercase) * 2
df = pd.DataFrame({'txt': np.random.choice(data, 30)})

-C10
做个模拟怎么样?很抱歉。我在最后一个
df
上添加了
.head(10)
,它与
grep-C
不太一样
grep-c3
-应该显示所有匹配的行,包括每个匹配前的3行和每个匹配后的3行(没有重复)。@nimbus3000,随时乐意帮助!
{ temp }  » grep -n -C 2 'a' aaa.txt                                                                          /d/temp
16-e
17-s
18:a
19-n
20-h
21-w
22-q
23:a
24-i
25:a
26-s
27-l
import string
data = list(string.ascii_lowercase) * 2
df = pd.DataFrame({'txt': np.random.choice(data, 30)})