Welcome to Westonci.ca, your go-to destination for finding answers to all your questions. Join our expert community today! Experience the convenience of getting accurate answers to your questions from a dedicated community of professionals. Our platform provides a seamless experience for finding reliable answers from a network of experienced professionals.

I want to merge rows by matching multiple ids. But in id terms, it can be substring. For example, "art" is a substring of "Earth" so, we will consider that's the same thing

Below example, I used Name and lname, phone, pin, as a ID.
Name and lname need to complete match and phone, pin can be partial match.

for example row number 0, 3 and 6 name and lname is complete match but phone and pin is partical match like "456b" and "789c" available in row number 0 same "eee" and "qqq" is available in row number 3. So, that's a partial match.

and in row number 4 name and lname is matched but there is no any match in phone and pin in row number 0, 3, or 6. So, we'll not merge row number 4

And we merge all other data like subjects

df = pd.DataFrame({'name': ['Raj', 'Hardik', 'Parth', 'Raj', 'Raj','parth', 'Raj'],
'lname': ['abc', 'Hardik', 'aaa', 'abc', 'abc','aaa', "abc"],
'phone': ['123a, 456b, 789c', '-', '777', '456b', '0000', '777', '789c'],
'pin': ['eee', '741', '852', 'qqq, www, eee', '789', '852', 'qqq'],
'Subjects': ['Maths', 'Science', 'English', 'Biology', 'Physics', 'Psychology', 'Hindi']})

df=
name lname phone pin Subjects
0 Raj abc 123a, 456b, 789c eee Maths
1 Hardik Hardik - 741 Science
2 Parth aaa 777 852 English
3 Raj abc 456b qqq, www, eee Biology
4 Raj abc 0000 789 Physics
5 parth aaa 777 852 Psychology
6 Raj abc 789c qqq Hindi

ans = pd.DataFrame({'name': ['Raj', 'Hardik', 'Parth', 'Raj'],
'lname': ['abc', 'Hardik', 'aaa', 'abc'],
'phone': ['123a, 456b, 789c', '-', '777', '0000' ],
'pin': ['qqq, www, eee', '741', '852', '789' ],
'Subjects': ['Maths, Biology, Hindi', 'Science', 'English, Psychology', 'Physics' ]})

name lname phone pin Subjects
0 Raj abc 123a, 456b, 789c qqq, www, eee Maths, Biology, Hindi
1 Hardik Hardik - 741 Science
2 Parth aaa 777 852 English, Psychology
3 Raj abc 0000 789 Physics