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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top