数据框字符串从一个到另一个匹配并替换为原始值

Data frame string matching from one to another and replace with original value

我有两个数据框,A 和 B。在 A 数据框中,两列 value 和 filed 。在 B 数据框中也有 value 和 filed 列。 我想将B的'value'列与'Filed'列的A相匹配将A的Filed替换为B的值 .

一个=

 Value                     Filed        

valid username            username
valid username           input_txtuserid
valid username           name
Password                 input_txtpassword
Password                 txtPassword
Login                     input_submit_log_in
Login                     loginid
LOG IN                    SIGNIN

B=

 Value                     Filed        

input_txtuserid           "JOHN"
input_txtpassword          "78945"
input_submit_log_in        "Sucessfully"
City                       "London"
PLACE                      "4-A avenue Street"
PHONE                      789456

我希望我的数据框看起来像这样:

C=

 Value                     Filed        

valid username            "JOHN"
Password                   "78945"
Login                       "Sucessfully"
City                       "London"
PLACE                      "4-A avenue Street"
PHONE                      789456

我会制作一个自定义函数来嵌入逻辑,然后按行将其应用于 DataFrame。

import pandas as pd

A = pd.DataFrame({
    'Value':['valid username','valid username','valid username'],
    'Filed':['username', 'input_txtuserid', 'name']
})

B = pd.DataFrame({
    'Value':['input_txtuserid'],
    'Filed':['JOHN']
})


#dictionary mapping A.Filed and A.Value
_map = dict(zip(A.Filed.values, A.Value.values))



#Function embedding the logic
#It uses _map to determine the correct "filed" string
def get_correct_value(row, _map=_map):
    new_value = _map[row.Value]
    filed = row.Filed
    return new_value, filed



#Make a new df from the result of B.apply(function)
C = B.apply(get_correct_value, axis=1, result_type='expand')
C.columns = ['Value','Filed']
print(C)

#            Value Filed
#0  valid username  JOHN