0

I have two dataframes, here are snippets of both below. I am trying to find and replace the artists names in the second dataframe with the id's in the first dataframe. Is there a good way to do this?

   id                fullName
0   1           Colin McCahon
1   2  Robert Henry Dickerson
2   3           Arthur Dagley

                                             Artists
0           Arthur Dagley, Colin McCahon, Maria Cruz
1  Fiona Gilmore, Peter Madden, Nicholas Spratt, ...
2                             Robert Henry Dickerson
3                                         Steve Carr

Desired output:

                                             Artists
0                                   3, 1, Maria Cruz
1  Fiona Gilmore, Peter Madden, Nicholas Spratt, ...
2                                                  2
3                                         Steve Carr
0

2 Answers 2

2

You mean check with replace

df1.Artists.replace(dict(zip(df.fullName,df.id.astype(str))),regex=True)
0                                     3, 1, Maria Cruz
1    Fiona Gilmore, Peter Madden, Nicholas Spratt, ...
2                                                    2
3                                           Steve Carr
Name: Artists, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

1

Convert your first dataframe into a dictionary:

d = Series(name_df.id.astype(str),index=name_df.fullName).to_dict()

Then use .replace():

artists_df["Artists"] = artists_df["Artists"].replace(d, regex=True)

1 Comment

That only replaces if, in replace(d), d is the whole string. so, replace("Arthur Dagley","3")) doesnt work. It would have to be replace("Arthur Dagley, Colin McCahon, Maria Cruz","3, Colin McCahon, Maria Cruz"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.