简体   繁体   中英

pandas str.contains match exact substring not working with regex boudry

I have two dataframes, and trying to find out a way to match the exact substring from one dataframe to another dataframe.

First DataFrame :

import pandas as pd
import numpy as np

random_data = {'Place Name':['TS~HOT_MD~h_PB~progra_VV~gogl', 'FM~uiosv_PB~emo_SZ~1x1_TG~bhv'], 
              'Site':['DV360', 'Adikteev']}
        
dataframe = pd.DataFrame(random_data)
print(dataframe)

Second DataFrame

test_data = {'code name': ['PB', 'PB', 'PB'], 
             'Actual':['programmatic me', 'emoteev', 'programmatic-mechanics'],
             'code':['progra', 'emo', 'prog']}

test_dataframe = pd.DataFrame(test_data)

Approach

for k, l, m in zip(test_dataframe.iloc[:, 0], test_dataframe.iloc[:, 1], test_dataframe.iloc[:, 2]):
    dataframe['Site'] = np.select([dataframe['Place Name'].str.contains(r'\b{}~{}\b'.format(k, m), regex=False)], [l],
                                  default=dataframe['Site'])

The current output is as below, though I am expecting to match the exact substring, which is not working with the code above.

Current Output:

Place Name                        Site
TS~HOT_MD~h_PB~progra_VV~gogl     programmatic-mechanics
FM~uiosv_PB~emo_SZ~1x1_TG~bhv     emoteev

Expected Output:

Place Name                        Site
TS~HOT_MD~h_PB~progra_VV~gogl     programmatic me
FM~uiosv_PB~emo_SZ~1x1_TG~bhv     emoteev

Data

import pandas as pd
 import numpy as np
 random_data = {'Place Name':['TS~HOT_MD~h_PB~progra_VV~gogl',
                                     'FM~uiosv_PB~emo_SZ~1x1_TG~bhv'], 'Site':['DV360', 'Adikteev']}
    dataframe = pd.DataFrame(random_data)

    test_data = {'code name': ['PB', 'PB', 'PB'], 'Actual':['programmatic me', 'emoteev', 'programmatic-mechanics'],
                 'code':['progra', 'emo', 'prog']}
    test_dataframe  = pd.DataFrame(test_data)

Map the test_datframe code and Actual into dictionary as key and value respectively

keys=test_dataframe['code'].values.tolist()

dicto=dict(zip(test_dataframe.code, test_dataframe.Actual))
dicto

Join the keys separated by |to enable search of either phrases

k = '|'.join(r"{}".format(x) for x in dicto.keys())
k

Extract string from datframe meeting any of the phrases in k and map them to to the dictionary

dataframe['Site'] = dataframe['Place Name'].str.extract('('+ k + ')', expand=False).map(dicto)
dataframe

Output

在此处输入图像描述

Not the most elegant solution, but this does the trick.

Set up data

import pandas as pd
import numpy as np
random_data = {'Place Name':['TS~HOT_MD~h_PB~progra_VV~gogl',
                                'FM~uiosv_PB~emo_SZ~1x1_TG~bhv'], 'Site':['DV360', 'Adikteev']}

dataframe = pd.DataFrame(random_data)

test_data = {'code name': ['PB', 'PB', 'PB'], 'Actual':['programmatic me', 'emoteev', 'programmatic-mechanics'],
             'code':['progra', 'emo', 'prog']}

test_dataframe = pd.DataFrame(test_data)

Solution

Create a column in test_dataframe with the substring to match:

test_dataframe['match_str'] = test_dataframe['code name'] + '~' + test_dataframe.code

print(test_dataframe)
  code name                  Actual    code  match_str
0        PB         programmatic me  progra  PB~progra
1        PB                 emoteev     emo     PB~emo
2        PB  programmatic-mechanics    prog    PB~prog

Define a function to apply to test_dataframe :

def match_string(row, dataframe):
    ind = row.name
    try:
        if row[-1] in dataframe.loc[ind, 'Place Name']:
            return row[1]
        else:
            return dataframe.loc[ind, 'Site']
    except KeyError:
        # More rows in test_dataframe than there are in dataframe
        pass

# Apply match_string and assign back to dataframe
dataframe['Site'] = test_dataframe.apply(match_string, args=(dataframe,), axis=1)

Output:

                      Place Name             Site
0  TS~HOT_MD~h_PB~progra_VV~gogl  programmatic me
1  FM~uiosv_PB~emo_SZ~1x1_TG~bhv          emoteev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM