Update my dataframe from a dictionary of list

Viewed 64

I have a dataframe with a certain ID and I also have a list dictionary. From the ID of the dataframe I need to update that ID if it exists in the dictionary but in the dictionary it is updated by two IDs, here are the examples: Dataframe

ID Name Last Name
1 Peter Smith
2 John Allen
7 Rene Watson
8 Dilan Foster

Dictionary

Dict = {1: [3, 4], 2: [5, 6], 7: [11], 8:[8]}

Expect Output:

ID Name Last Name
3 Peter Smith
4 Peter Smith
5 John Allen
6 John Allen
11 Rene Watson
8 Dilan Foster

Thanks in advance!

1 Answers

Let us try map and explode

df.assign(ID=df['ID'].map(dct)).explode('ID', ignore_index=True)

  ID   Name Last Name
0  3  Peter     Smith
1  4  Peter     Smith
2  5   John     Allen
3  6   John     Allen
Related