我想删除文章" the ","a“或"an”从字符串的开头与python (熊猫)的dataframe列。我尝试的所有选项都是从字符串中的任何位置删除的,但我只想在一开始就删除它们。并不是列中的所有字符串都包含相同的项目,甚至有些列数据也没有这些项目。我使用了不同的方法,例如替换、删除字符串的部分,并试图查看是否可以在开始时删除特定数目的字符。
提前谢谢你的帮助。
很抱歉,如果这是一个类似的问题,我试图找到一个可以回答我的具体用例,但我只找到了数据之间的精确匹配。
我有两个熊猫数据框架的描述:
df1:
Description
i had lunch
going to the airport
buying a suitcase
df2:
Description
buying lunch
airport travel
owning a car
我想筛选和/或计算df2有多少次在df1的任何行中出现匹配的单词
例如,df2有“午餐”和“机场”这两个单词,而这些单词出现在df1中,所以我想取出并计数df2中有匹配项的行。
因此,我的输出只是一个过滤的df2,基
Dataframe看起来像这个
col_a
Python PY is a general purpose PY language
Programming PY language in Python PY
Its easier to understand PY
The syntax of the language is clean PY
这段代码我试图实现这个功能,但无法获得预期的输出。如果有什么帮助的话。
下面是使用正则表达式的下面的代码:
df['col_a'].str.extract(r"([a-zA-Z'-]+\s+PY)\b")
期望输
我对Python完全是个新手,只是在学习一些我用过的用例。 我有两个数据框,一个是我需要Country列中的值的地方,另一个是名为'Countries‘的列中的值,它需要在主数据框中映射到名为' Data’的列。(如果这个问题已经回答了,请接受我的道歉) 下面是主要的DataFrame: Name Data | Country
----------------------------- | ---------
Arjun Kumar Reddy las Vegas |
Divya london Khosla |
在一个长文本文件中,有几个标题。
我需要分割这个文件,这样我就可以将每个标题和内容分开。
重复的头将被视为一个。最起码的例子:
HeaderA
example text
HeaderB
example text
HeaderC
example text
HeaderC
example text
HeaderD
example text
在python中使用这个正则表达式,我管理了以下内容:
Header(\w)[\s\S]*?(?=Header(?!\1)|$)
请注意,两个HeaderC都是作为一个组捕获的。
现在,我收到了一个稍微修改过的文本文件,如下所示:
Header
foo
我在R中有两个dataframe,我想使用dataframe "y“这样的参数对dataframe "x”进行查询。
我有这样的代码:
x <- c('The book is on the table','I hear birds outside','The electricity
came back')
x <- data.frame(x)
colnames(x) <- c('text')
x
y <- c('book','birds','ele
我有一个数据格式,如下所示: pandas.core.frame.DataFrame
newdf = pd.DataFrame([
[0, "Hello, How are you?", 1],
["I wish, we could get it right", 0, 0],
[0, "Hey there, hope you are doing well", 0]],
columns=['Mon','Tue','Wed'])
newdf
|Mon
我正在尝试过滤包含两个单词的所有行:mom和dad。
Family
My mom is a teacher.
My dad is a policeman.
Both my mom and dad are retired.
我的预期产出是
Both my mom and dad are retired
因为它包含了两个词。我尝试过使用str.contains()。只是想知道是否有另一种方法可以做到。
s = df.Family
searchfor = ['mom', 'dad']
found = [s.str.contains(x) for x in sear
我在VS中运行一个c++程序。我提供了一个regex,我正在解析一个文件,该文件长度超过200万行,用于匹配该regex的字符串。以下是代码:
int main() {
ifstream myfile("file.log");
if (myfile.is_open())
{
int order_count = 0;
regex pat(R"(.*(SOME)(\s)*(TEXT).*)");
for (string line; getline(myfile, line);)
这是对这个SO post的后续,它提供了一个在字符串列中替换文本的解决方案。
df‘’range‘=df’‘range’.str.替换( ',‘,'-')
然而,这似乎不适用于双句点或后跟句号的问号。
testList = ['this is a.. test stence', 'for which is ?. was a time']
testDf = pd.DataFrame(testList, columns=['strings'])
testDf['strings'].str.replac
在我的word文件中包含了一些不同数字的要求,比如"SWS_Rte_01252、SWS_Rte_01253、.I“,.I想收集所有这些需求,因为(SWS_Rte_01252,SWS_Rte_01253)我为此编写了一个python脚本,但是它不能正常工作,对这个问题有什么建议?
import re
import docx
regex = r"\\[SWS_Rte_*\\]\\ \\|"
doc = docx.Document('selection5.docx')
f = open('Res.txt',"w")
for pa