0

Here is my issue, I have two pandas dataframes, all with the same column names, and the same number of rows. Below is a sample of what each dataframe would look like. Pandas Dataframe Example

Let me call my dataframes DF1 and DF2. I need to take the "PosRank" value from DF1 and set it equal to the "PosRank" of DF2 at the row where the "names" are the same. There is no guarantee they will be in any sorted or matching order but all names in DF1 will also be in DF2 and visa versa, and will only appear once in each.

AMC
  • 2,642
  • 7
  • 13
  • 35
  • 1
    Please see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – roganjosh May 02 '20 at 23:17
  • Please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC May 02 '20 at 23:42
  • I forgot to mention: _Here is my issue_ No, that's not an issue, that is a goal or objective. What we need to know is what specifically is stopping you from reaching that goal. – AMC May 03 '20 at 00:07
  • Was this of help? – wwnde May 03 '20 at 05:51

1 Answers1

0

There are many ways of doing it. Pay attention to the links in comment

Lets try this one where we use Series.map() method.

df1=pd.DataFrame({'Name':['DeAngelo William','Michael Turner'],'PosRank':[1.0,2.0]})

df1

enter image description here

df2=pd.DataFrame({'Name':['DeAngelo William','Michael Turner'],'PosRank':[3.0,6.0]})

df2

enter image description here

Let us create a dictionary with Name from df2 as key and PosRank from df2 as value

d=dict(zip(df2.Name,df2.PosRank))

Now map values from df2 to df1

df1.PosRank=df1.Name.map(d)

New df1

enter image description here

wwnde
  • 26,119
  • 6
  • 18
  • 32