I have two dfs: myteam and allplayers.
In myteam df, the names are mentioned:
myteam = pd.DataFrame({
"name": ["ryan", "bill", "saka", "Henry","Rooney"],
"name1": ["ryan 112", "Bill Matt Cdevaca", "Bukayo Saka", "Super Henry","Rooney"],
"name2": [np.nan, "XXVaca", "Bukayo", "Thierry","Rooney"],
"name3": ["Ryan", "XXVaca", "Sak", "Thierr","Roon"]})
In allplayers df, we have goals and matches for all the league players.
allplayers = pd.DataFrame({
"player_name": ["RYAN 112", "xxVaca", "bukayo", "thierry", "RONALDO", "MESSI"],
"goals": [0, 2, 5, 10, 100, 200],
"matches": [22, 100, 200, 300, 100, 90]})
The name pattern for players does not match in the two dfs.
I want to select one row at a time from myteam df and iterate over "name3", "name1" and "name2" values in that order (if the prior name fails go to next one) to check if the value is equal (case-insensitive) to player_name col value in allplayers df and return the goals and matches from allplayers df and cbind the name value from myteam to the answerdf.
If name3 value is found then no need to check for name1 and so on...
If none of the values in name3, name1, name2 are equal, then return only the myteam player name with NAN in goals and matches columns.
answerdf = pd.DataFrame({
"player_name": ["RYAN 112", "xxVaca", "bukayo", "thierry", "Rooney"],
"goals": [0, 2, 5, 10, "NaN"],
"matches": [22, 100, 200, 300, "NaN"],
"name_from_myteam_df": ["ryan", "bill", "saka", "Henry", "Rooney"]})