Python: Pandas Найти наличие текста внутри Pandas (Dataframe) - PullRequest
0 голосов
/ 05 марта 2019

У меня есть два столбца ColA ColB в кадре данных панд, я хочу сравнить ColB с ColA, если colA содержит совпадающее слово с colB, тогда я должен обновить colC как доступный.

If it not macthes print not available.
ColA                                                            ColB  
You can extract_insights on product reception                   insights
user various sources like extract_insights etc.                 insights   
some other sourced mail by using signals from state art         text       

Примечание: даже еслиСтолбец A содержит любые специальные символы, однако он должен иметь возможность идентифицировать текст colB

Требуемый вывод:

If it not macthes print not available.
ColA                                                           ColB     Colc
You can extract_insights on product reception                  insights AVB
user various sources like extract_insights etc.                insights AVB  
some other sourced mail by using signals from state art        text     NAVB  

1 Ответ

1 голос
/ 05 марта 2019

Попробуйте следующее:

import pandas as pd

# Initialize example dataframe
data = [
    ["You can extract_insights on product reception", "insights"],
    ["user various sources like extract_insights etc.", "insights"],
    ["some other sourced mail by using signals from state art", "text"],
]
df = pd.DataFrame(data=data, columns=["ColA", "ColB"])

# Create column C with comparison results
df["ColC"] = [
    "AVB" if (b in a) else "NAVB"
    for (a, b) in zip(df["ColA"], df["ColB"])
]

print(df)
# Output:
#                                                 ColA      ColB  ColC
# 0      You can extract_insights on product reception  insights   AVB
# 1    user various sources like extract_insights etc.  insights   AVB
# 2  some other sourced mail by using signals from ...      text  NAVB
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...