0

I have been searching for this answer for a day now. I can't find a solution. I have two data frames for NBA stats. One has just team names and the other has team names and stats associated. I want to look up each team name on the first dataframe and append the stats data from the second one to the rows that match that team name.

df1 = 'DATE' : [rows of dates], 'TEAM_NAME': [row of team names with duplicates]
df2 = 'TEAM_NAME': [row of unique team names], 'STAT #1' ['row of stats], etc....

I want df1 to look something like this:

'DATE'   'TEAM_NAME'  'STAT 1' 'STAT 2' etc...
1-Jan-21  Boston        23        15    
5-Jan-21  Detroit       45        90
1-Jan-21  Boston        23        15

UPDATE* I used merge and join and it does do exactly what i need, there is one problem. Both functions group my data by team names as it encounters them to join. I need it to retain the original order and replace the rows that way.

  • 1
    Please read about how to [write a good question](https://stackoverflow.com/a/20159305/13876078) so that others find it easier to help you. – mullinscr Feb 07 '21 at 19:46
  • Welcome to SO. When asking questions, you should provide a small example which allows people to recreate your problem. Also, you need to look at the pandas join operation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html – mcsoini Feb 07 '21 at 19:48

1 Answers1

0

Instead of individually pulling the values from the cells, just merge the two data frames.

merged = df1.merge(df2,how='left')

Because both df1 and df2 have 'TEAM_NAME' columns, the merge operation will automatically merge on those values, assuming those are the only shared columns between the two dataframes. If the frames have more than one shared column, specify the join columns;

merged = df1.merge(df2,how='left',on='TEAM_NAME')

And if you need to merge on both date and team_name columns;

merged=df1.merge(df2,how='left',on=['TEAM_NAME','DATE'])

but to answer your original question, cells can be retrieved by value like so:

stat1 = df2.loc[df2.TEAM_NAME=='LAKERS','STAT#1']
Boskosnitch
  • 774
  • 3
  • 8
  • @CodeNoob23 You're welcome! Here's a concise tutorial on joining dataframes that I often referenced when I was first learning: https://towardsdatascience.com/combining-pandas-dataframes-the-easy-way-41eb0f2c1ebf And here's a good tutorial on indexing/selecting values https://www.tutorialspoint.com/python_pandas/python_pandas_indexing_and_selecting_data.htm – Boskosnitch Feb 07 '21 at 20:07