PANDAS在字符串的一列中找到确切的单词和单词之前的内容,并将新的列附加到python(pandas)列中 [英] PANDAS Finding the exact word and before word in a column of string and append that new column in python (pandas) column
问题描述
在col_a中找到目标词和前一个词,并在col_b_PY和col_c_LG列中附加匹配的字符串
This code i have tried to achive this functionality but not able to
get the expected output. if any help appreciated
Here is the below code i approach with regular expressions:
df[''col_b_PY']=df.col_a.str.contains(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+)
{0,1}PY")
df.col_a.str.extract(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,1}PY",expand=True)
数据框看起来像这样
col_a
Python PY is a general-purpose language LG
Programming language LG in Python PY
Its easier LG to understand PY
The syntax of the language LG is clean PY
所需的输出:
col_a col_b_PY col_c_LG
Python PY is a general-purpose language LG Python PY language LG
Programming language LG in Python PY Python PY language LG
Its easier LG to understand PY understand PY easier LG
The syntax of the language LG is clean PY clean PY language LG
推荐答案
您可以使用
df['col_b_PY'] = df['col_a'].str.extract(r"([a-zA-Z'-]+\s+PY)\b")
df['col_c_LG'] = df['col_a'].str.extract(r"([a-zA-Z'-]+\s+LG)\b")
或者,提取所有匹配项并将它们与空格连接:
Or, to extract all matches and join them with a space:
df['col_b_PY'] = df['col_a'].str.extractall(r"([a-zA-Z'-]+\s+PY)\b").unstack().apply(lambda x:' '.join(x.dropna()), axis=1)
df['col_c_LG'] = df['col_a'].str.extractall(r"([a-zA-Z'-]+\s+LG)\b").unstack().apply(lambda x:' '.join(x.dropna()), axis=1)
Note you need to use a capturing group in the regex pattern so that extract
could actually extract the text:
在正则表达式 pat 中提取捕获组作为DataFrame中的列.
Extract capture groups in the regex pat as columns in a DataFrame.
请注意,\b
单词边界对于将PY
/LG
整个单词匹配是必需的.
Note the \b
word boundary is necessary to match PY
/ LG
as a whole word.
此外,如果您只想从一个字母开始比赛,则可以将模式修改为
Also, if you want to only start a match from a letter, you may revamp the pattern to
r"([a-zA-Z][a-zA-Z'-]*\s+PY)\b"
r"([a-zA-Z][a-zA-Z'-]*\s+LG)\b"
^^^^^^^^ ^
其中[a-zA-Z]
将匹配一个字母,而[a-zA-Z'-]*
将匹配0个或多个字母,撇号或连字符.
where [a-zA-Z]
will match a letter and [a-zA-Z'-]*
will match 0 or more letters, apostrophes or hyphens.
带有熊猫0.24.2的Python 3.7:
Python 3.7 with Pandas 0.24.2:
pd.set_option('display.width', 1000)
pd.set_option('display.max_columns', 500)
df = pd.DataFrame({
'col_a': ['Python PY is a general-purpose language LG',
'Programming language LG in Python PY',
'Its easier LG to understand PY',
'The syntax of the language LG is clean PY',
'Python PY is a general purpose PY language LG']
})
df['col_b_PY'] = df['col_a'].str.extractall(r"([a-zA-Z'-]+\s+PY)\b").unstack().apply(lambda x:' '.join(x.dropna()), axis=1)
df['col_c_LG'] = df['col_a'].str.extractall(r"([a-zA-Z'-]+\s+LG)\b").unstack().apply(lambda x:' '.join(x.dropna()), axis=1)
输出:
col_a col_b_PY col_c_LG
0 Python PY is a general-purpose language LG Python PY language LG
1 Programming language LG in Python PY Python PY language LG
2 Its easier LG to understand PY understand PY easier LG
3 The syntax of the language LG is clean PY clean PY language LG
4 Python PY is a general purpose PY language LG Python PY purpose PY language LG
这篇关于PANDAS在字符串的一列中找到确切的单词和单词之前的内容,并将新的列附加到python(pandas)列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!