Append a pandas row with data from another dataframe if a certain column matches
Question:
I have two dataframes with the same columns (they represent different years of a sporting season). If a player played in both seasons, I’d like to append certain information from the following season to that season’s dataframe.
DF1
Name
PPG
Michael Jordan
31.7
DF2
Name
PPG
Michael Jordan
28.4
What I’d like to do is combine them (either into DF1 or a new DF3) and have three rows
Name
PPG
PPG Next Season
Michael Jordan
31.7
28.4
Not all players played in both seasons. How can I check all the players in DF1, see if they played in DF2, and if so add a new column to DF1 tracking those players DF2 PPG?
Answers:
import pandas as pd
df1 = pd.DataFrame({'Name': ['Michael Jordan'], 'PPG': [31.7]})
df2 = pd.DataFrame({'Name': ['Michael Jordan'], 'PPG': [28.4]})
df3 = df1.merge(df2, on='Name', suffixes=('', ' Next Season'))
print(df3)
The suffixes
parameter is used to add a suffix to the columns in df2
to avoid duplicate column names in the merged dataframe.
Name
PPG
PPG Next Season
Michael Jordan
31.7
28.4
I have two dataframes with the same columns (they represent different years of a sporting season). If a player played in both seasons, I’d like to append certain information from the following season to that season’s dataframe.
DF1
Name | PPG |
---|---|
Michael Jordan | 31.7 |
DF2
Name | PPG |
---|---|
Michael Jordan | 28.4 |
What I’d like to do is combine them (either into DF1 or a new DF3) and have three rows
Name | PPG | PPG Next Season |
---|---|---|
Michael Jordan | 31.7 | 28.4 |
Not all players played in both seasons. How can I check all the players in DF1, see if they played in DF2, and if so add a new column to DF1 tracking those players DF2 PPG?
import pandas as pd
df1 = pd.DataFrame({'Name': ['Michael Jordan'], 'PPG': [31.7]})
df2 = pd.DataFrame({'Name': ['Michael Jordan'], 'PPG': [28.4]})
df3 = df1.merge(df2, on='Name', suffixes=('', ' Next Season'))
print(df3)
The suffixes
parameter is used to add a suffix to the columns in df2
to avoid duplicate column names in the merged dataframe.
Name | PPG | PPG Next Season |
---|---|---|
Michael Jordan | 31.7 | 28.4 |