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?
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.
Comments