Why are all values NaN after mapping 'player_name' column in Pandas Data Frame?

AI Thread Summary
The discussion revolves around the challenge of transferring the 'player_name' column from one DataFrame (df1) to another (df2) using a common key, 'player_api_id'. The initial attempt to map 'player_name' directly resulted in NaN values due to a likely mismatch in the keys. It was clarified that the two DataFrames are not the same size but share a primary key. The solution proposed involves creating a dictionary that pairs 'player_api_id' with 'player_name' from df1, which is then used to map the names to df2. This approach successfully resolves the issue, allowing the 'player_name' column to be populated correctly in df2.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have two data frames df1 and df2

df1 has two columns 'player_name' and 'player_id'.

Similarly df2 has 'player_id' column.

From this configuration I want to pass 'player_name' column to df2 by using 'player_id'. For this reason I have tried something like this,

Code:
df2['player_name'] = df2['player_api_id'].map(df1['player_name'])

The code runs without error and I obtain 'player_name' column in df2 but all the values are NaN. I did not understand why this happens.
 
Technology news on Phys.org
Two questions. Are the two dataframes the same size and do they share a column that acts like a primary key? If so, then I would use a merge with just the column that you want to add and its key from the other dataframe.
 
You can look at the data from here

https://www.kaggle.com/hugomathien/soccer

I am only interested in Player and Player_Attributes datas. In those data as you can see there are two columns that has the same name; player_api_id.

So as I have said before I want to move player_name from the Player data to Player_Attributes by using the player_api_id.

Borg said:
Are the two dataframes the same size
Nope

Borg said:
ey share a column that acts like a primary key?
I guess so
 
Sorry, I was responding from my phone and didn't read closely enough.
Arman777 said:
df2['player_name'] = df2['player_api_id'].map(df1['player_name'])
If you want to add player names to df2 from df1, you would need to replace the df1['player_name'] part with a dictionary of IDs and player names from df1. Assuming that the Player table has no duplicates, something like this:
player_name_dictionary = dict(zip(df1.player_api_id, df1.player_name)) df2['player_name'] = df2['player_api_id'].map(player_name_dictionary)
 
Borg said:
Sorry, I was responding from my phone and didn't read closely enough.

If you want to add player names to df2 from df1, you would need to replace the df1['player_name'] part with a dictionary of IDs and player names from df1. Assuming that the Player table has no duplicates, something like this:
player_name_dictionary = dict(zip(df1.player_api_id, df1.player_name)) df2['player_name'] = df2['player_api_id'].map(player_name_dictionary)
thanks a lot. It works
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top