сопоставление данных в Python с элементами списка - PullRequest
1 голос
/ 05 ноября 2019

Имеют фрейм данных 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",,,,]

Для фрейма данных необходимо добавить новый column B, соответствующий строкам из списка

новый фрейм данных с соответствующим ключевым словом, добавленный в качестве нового столбца

введите описание изображения здесь

matches= "|".join(f"\\b{i}\\b" for i in matches)
df["B"] = df['text'].str.findall(matches,re.IGNORECASE).str.join("|")
df["B"]

1 Ответ

0 голосов
/ 05 ноября 2019

Попробуйте это:

Содержимое 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

, что является ожидаемым результатом!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...