Pandas iterate through comma separated string and return lookup values joined together

Viewed 16
Name Ids
Bob '1','2'
Tom '3','4'

If my lookup value is "Bob, Tom" I want to return each of the IDs values and joined by a comma so

[In] Bob,Tom

[Out] '1','2','3','4'

1 Answers

Check the code below

data = {'Name': ['Bob', 'Tom','Jimmy'],
        'Ids': ["'1','2'", "'3','4'", "'5','6'"]}
df = pd.DataFrame(data)
input_index = df['Name'].str.contains('Bob|Tom')

','.join(df[input_index].Ids) // '1','2','3','4'
Related