Answer the question
In order to leave comments, you need to log in
How to replace data in Data frame?
I have two csv files. In the first - just a column with the names of objects (old names). The second one has two columns: 1st column - old names, 2nd column - new names of objects.
It is necessary to make sure that the names in the first file are changed to new ones (those in the second column). And if this object is not in the second file, then it (the object) has retained its old name. It turns out you need to do something like VLOOKUP in excel. Thanks
Answer the question
In order to leave comments, you need to log in
Let's say there are 2 files:
df1 = pd.DataFrame({'name': ['a', 'b', 'c']})
df2 = pd.DataFrame({'old_name': ['a', 'b'], 'new_name': ['a1', 'b1']})
df3 = pd.merge(df1, df2, left_on='name', right_on='old_name', how='left')
df3.loc[df3['new_name'].isnull(), 'new_name'] = df3.loc[df3['new_name'].isnull(), 'name']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question