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

In summary, there are two dataframes, df1 and df2, with df1 having 'player_name' and 'player_id' columns and df2 having a 'player_id' column. The goal is to pass the 'player_name' column from df1 to df2 using the 'player_id' column. The code used was df2['player_name'] = df2['player_api_id'].map(df1['player_name']), but it resulted in all values being NaN. This may be due to the dataframes not being the same size and not sharing a primary key column. To solve this, a dictionary was created using the 'player_api_id' and 'player_name' columns from df1, and then mapped
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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.
 
  • #3
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
 
  • #4
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)
 
  • Like
Likes Arman777
  • #5
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
 
  • Like
Likes Borg

What is a Pandas Data Frame Mapping?

A Pandas Data Frame Mapping is a process of creating a visual representation of a data frame in Pandas, using various mapping techniques and tools.

How do I create a Pandas Data Frame Mapping?

To create a Pandas Data Frame Mapping, you can use the built-in mapping functions and methods in Pandas or external libraries such as Matplotlib or Seaborn. You can also customize your mapping by specifying different parameters and settings.

What are the benefits of using Pandas Data Frame Mapping?

Pandas Data Frame Mapping allows you to easily visualize and analyze complex data sets, identify patterns and trends, and communicate your findings effectively. It also helps in identifying missing or erroneous data, making data cleaning and preprocessing easier.

Can I save my Pandas Data Frame Mapping as an image or file?

Yes, you can save your Pandas Data Frame Mapping as an image or file by using the "savefig" function in Matplotlib or the "save" function in Seaborn. You can also specify the file format, resolution, and other parameters when saving the mapping.

Are there any limitations to using Pandas Data Frame Mapping?

Pandas Data Frame Mapping may not be suitable for all types of data and may not provide accurate results if the data set is too large or complex. It also requires some knowledge of data analysis and mapping techniques to create meaningful visualizations.

Similar threads

  • Programming and Computer Science
Replies
8
Views
955
  • Programming and Computer Science
2
Replies
42
Views
6K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
916
  • Programming and Computer Science
Replies
29
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
595
  • Programming and Computer Science
Replies
18
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
10
Views
25K
Replies
1
Views
962
Back
Top