I have programmed the following functionality:
The function returns True, when the two strings match sequentially except for a "*" value and false when they differ by at least 1 character.
def matching(row1, row2):
string = row1['number']
sub_string = row2['number']
flag = True
i=0
if len(string) == len(sub_string):
while i < len(string) and flag==True:
if string[i] != "*" and sub_string[i] != "*":
if string[i] != sub_string[i]:
flag = False
i+=1
else:
flag = False
return flag
Assuming I have a dataframe with the column 'number'. I want to apply this function to a dataframe in order to obtain the following format:
| number | unique_id |
| ------ | --------- |
| 178*A8 | 0 |
| 13**B4 | 1 |
| 17***8 | 0 |
| 82819B | 2 |
| 13**B4 | 1 |
I managed to write the unique_id with the following code, but it only works when the numbers match. I would like to perform the same functionality but using the function in the code first.
df['unique_id'] = pd.factorize(df['number'])[0]
| number | unique_id |
| ------ | --------- |
| 178*A8 | 0 |
| 13**B4 | 1 |
| 17***8 | 2 |
| 82819B | 3 |
| 13**B4 | 1 |
Edited: We will assume that matching will be done on a first-come, first-served basis. If the first value is 123*, all numbers matching('123*',X) == True will be assigned the same id.