[英]Python find word in a rows and append this in a new text file
I use jupyter notebook with pandas, I would like to find a recurring word of my choice in a large file, then select the rows and paste it or append on another text file, eg with the word " test ".:我将 jupyter notebook 与 pandas 一起使用,我想在一个大文件中找到我选择的重复出现的单词,然后选择行并粘贴它或附加到另一个文本文件上,例如使用单词“ test ”。:
this is a test sample line
this is a second example line
this is a third example line
this is a test fourth sample line
this is a final example line
and get on a new text file only the lines in which the word " test " is present:并在一个新的文本文件中只出现“ test ”一词的行:
this is a test sample line
this is a test fourth sample line
How could I achieve this in python using jupyter to make things easier?我如何使用 jupyter 在 python 中实现这一点,让事情变得更容易?
PS.附注。 it would be perfect if you could read from multiple text files and append the rows without overwriting them!
如果您可以从多个文本文件中读取并附加行而不覆盖它们,那将是完美的!
Thanks as always!一如既往的感谢!
Assuming the following dataframe as input:假设以下数据帧作为输入:
col
0 this is a test sample line
1 this is a second example line
2 this is a third example line
3 this is a test fourth sample line
4 this is a final example line
You could use str.contains
:你可以使用
str.contains
:
df[df['col'].str.contains(r'\btest\b', regex=True)]
output:输出:
col
0 this is a test sample line
3 this is a test fourth sample line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.