How to check string of one column ,and change of string of another column using pandas

Cherry

I have big csv file, PF sample data like below

Name,value,data
jack,X16206,hi this is X16206
Riti,X1620600,I want to change X16206.
Aadii,X16206,New value is X1620600.
jan,abc700134,something new 20600.

I have a value X16206(alpha-numeric) with 00 added sometimes and sometimes not, in value column and data column
I want to check the string from value column and change the string present in a sentence which is in the data column as 'exact'

expected output:

Name,value,data
jack,X16206,hi this is [exact]
Riti,X1620600,I want to change [exact].
Aadii,X16206,New value is [exact].
jan,abc700134,something new 20600.

what I have tried so far

df1['num'] = np.where(df1['value'].str.len().isin({6,8}), 1, -1)
def myfn2(row):
    if row['num']==1:
        row['New_data']=row['data'].replace(row['value'],'[exact]')
    else:
        row['New_data']=row['data']
    return row
    
df1=df1.apply(myfn2,axis=1)

Output I got

Name,value,data,num,New_data
jack,X16206,hi this is X16206,1,hi this is [exact]
Riti,X1620600,I want to change X16206,1,I want to change X16206.
Aadii,X16206,New value is X1620600,1,New value is [exact]00.
jan,abc700134,something new 20600,-1,something new 20600.

Can anyone please help me how to do this?

Andrej Kesely

Try:

import re


def fn(x):
    v = re.sub(r"(?<=\d{4})00$", "", x["value"])
    return re.sub(r"(" + v + "0?0?)", r"[exact]", x["data"])


df["data"] = df.apply(fn, axis=1)
print(df)

Prints:

    Name      value                       data
0   jack     X16206         hi this is [exact]
1   Riti   X1620600  I want to change [exact].
2  Aadii     X16206      New value is [exact].
3    jan  abc700134       something new 20600.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check if a string in a column is a sub-string in another column using dataframe and pandas

Check if string is in another column pandas

Python Pandas: Check if string in one column is contained in string of another column in the same row

how to remove substring of a column if it matches with string of another column using pandas?

Pandas: Determine if a string in one column is a substring of a string in another column

Remove string from one column if present in string of another column pandas

Pandas - For each group, if string in one column is in another column, add to column

Check if at least one column contains a string in pandas

How to check and remove if string in one column matches with string in another column in R

Pandas / Check if a string is in a column

Check if string in one column is contained in string of another column in the same row and add new column with matching column name

pandas dataframe check if column contains string that exists in another column

Check if string-sliced characters in Pandas column contain another string

How to insert a string column to another string column in pandas dataframe?

Pandas: check if string value in one column is part of string of another column in same row of dataframe - current script returning all Yes

How to change data in a column using the longer length of a string in PANDAS?

Change value of a column based on whether string is in another column using R

How to replace a substring with another string in a column in pandas

How to combine string from one column to another column at same index in pandas DataFrame?

How to drop entire row if string of one column contains the word from another column in pandas dataframe

find position of column string in another column using Pandas

Split a python pandas column using positional string in another column

how to highlight sequential string in one column based on another column

How to determine if a string from one column is in another column?

How to replace a substring in one column based on the string from another column?

Check string values in one column from one df to another column in another df

Python - Is one string column in another?

Python Pandas how to update a column if another column contains a certain string

Replace a string form one column in another column