Python Data frame Find and replace column values from a list

Viewed 18

I have a data frame with 4 columns. i will typically have 200 or more so rows. i have an example below showing 4 rows as an example. There is a column for account number. this account number may appear multiple times in the column. i have a separate excel sheet with 2 columns, listing account number and account name. I want to replace the account number with the corresponding account name shown on my excel sheet. I cannot manually type out using the replace function for every account number, as there are hundreds of account names and numbers. is there a way i can replace the account number with their relevant account names? or perhaps append a new column showing the relevant account name?

1 Answers

If I understand correctly, you want something like the following (You can get l1 and l2 by parsing the excel sheet):

import pandas as pd

l1 = [100, 200]
l2 = [1000, 2000]

z = pd.DataFrame({"one": [100,100,300,200], "two": [100,100,300,200]})
"""
   one  two
0  100  100
1  100  100
2  300  300
3  200  200
"""
print(z)

z.two.replace(l1, l2, inplace=True)
"""
   one   two
0  100  1000
1  100  1000
2  300   300
3  200  2000
"""
print(z)

Related