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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top