0

How can I transfer the links from file2.xlsx to the matching names in file1.xlsx using pandas?

file1.xlsx

0 Names Ratings Links
1 Joe   10
2 Jack  7
3 Jim   8

file2.xlsx

  Names Links
0 Jim   example.com/32145
1 Joe   example.com/35235
2 Jack  example.com/90234

New file1.xlsx

  Names Ratings Links
0 Joe   10      example.com/35235
1 Jack  7       example.com/90234
2 Jim   8       example.com/32145
  • `df1.merge(df2, on='Names')` or `df1.drop('Links', axis=1).merge(df2, on='Names')` –  Dec 05 '21 at 02:03
  • 2
    Do you know how to read the excel files into dataframes using pandas? –  Dec 05 '21 at 02:04

1 Answers1

0

If your first dataframe contains the Links column, you'll want to remove it:

df1.drop('Links', axis=1, inplace=True)

Then,

new_df = df1.merge(df2, on='Names')

Output:

>>> new_df
  Names  Ratings              Links
0   Joe       10  example.com/35235
1  Jack        7  example.com/90234
2   Jim        8  example.com/32145