python数据框与列表项匹配

具有一个包含df的数据框column (A)如下  A

["User mapping missing constantly for random users on product PLA-ZA. Hi,Generally,these results look encouraging. Here's what I see at the moment:- On Sept 12,when you ran this script,exactly the same users were shown as not "in sync" as we see in the most recent output."

"History ------------ *** Audit: MLACAMBR 01/10/2018 18:40:05 GMT ","1. find the process ids by doing ps -efl | grep BEMS74397","kill each of these processes,as follows (for example,if the process ID is 555555):","Troubleshoots from the KMCas well as from the sensor where the connections occurred"]

和Python列表匹配

["PLA-ZA","BEMS","MLACAMBR","KMC","OWL",]

Dataframe 需要添加一个新的column B来匹配列表中的字符串

具有匹配关键字的新数据框已添加为新列

enter image description here

matches= "|".join(f"\\b{i}\\b" for i in matches)
df["B"] = df['text'].str.findall(matches,re.IGNORECASE).str.join("|")
df["B"]
wx071134 回答:python数据框与列表项匹配

尝试一下:

匹配前df的内容:(显示输入df)

print(df)
                                             ColumnA
0  User mapping missing constantly for random use...        
1  History ------------ *** Audit: MLACAMBR 01/10...        
2  1. find the process ids by doing ps -efl | gre...        
3  kill each of these processes,as follows (for ...        
4  Troubleshoots from the KMCas well as from the ...        

代码:

df['ColumnB'] = ''
matches = ["PLA-ZA","BEMS","MLACAMBR","KMC","OWL"]

for index,row in df.iterrows():
    current_string = row['ColumnA']

    #This block of code extracts all matches that are present in ColumnA of every row in df
    tmpmatch = []
    for x in matches:
        if x in current_string and x not in tmpmatch:
            tmpmatch.append(x)
        tmp_matched_str = ','.join(tmpmatch)
    #Append  the matched string from matches into ColumnB of df
    df.loc[index,'ColumnB'] = tmp_matched_str

输出:

print(df)
                                             ColumnA   ColumnB
0  User mapping missing constantly for random use...    PLA-ZA
1  History ------------ *** Audit: MLACAMBR 01/10...  MLACAMBR
2  1. find the process ids by doing ps -efl | gre...      BEMS
3  kill each of these processes,as follows (for ...          
4  Troubleshoots from the KMCas well as from the ...       KMC

这是预期的输出!

本文链接:https://www.f2er.com/3160356.html

大家都在问