1

So I have multiple different dataframes, all with varying team names under the same column name, 'Team'. I've created a function to run through these all at once, but for some reason it doesn't execute.

def rename(df):
    df = df.replace({'Team':
                  {'NewEngland' : 'Patriots',
                   'GreenBay' : 'Packers',
                   'Pittsburgh' : 'Steelers',
                   'KansasCity' : 'Chiefs',
                   'Denver' : 'Broncos',
                   'Seattle' : 'Seahawks',
                   'Indianapolis' : 'Colts',
                   'New Orleans' : 'Saints',
                   'NewOrleans' : 'Saints',
                   'Dallas' : 'Cowboys',
                   'Baltimore' : 'Ravens',
                   'Philadelphia' : 'Eagles',
                   'Cincinnati' : 'Bengals',
                   'Carolina' : 'Panthers',
                   'Tennessee' : 'Titans',
                   'Arizona' : 'Cardinals',
                   'Buffalo' : 'Bills',
                   'SanFrancisco' : '49ers',
                   'Minnesota' : 'Vikings',
                   'Washington' : 'Redskins',
                   'Chicago' : 'Bears',
                   'Atlanta' : 'Falcons',
                   'NYGiants' : 'Giants',
                   'NYJets' : 'Jets',
                   'Cleveland' : 'Browns',
                   'Detroit' : 'Lions',
                   'Miami' : 'Dolphins',
                   'TampaBay' : 'Buccaneers',
                   'Jacksonville' : 'Jaguars',
                   'Houston' : 'Texans',
                   'HoustonTexans' : 'Texans',
                   'Oakland' : 'Raiders',
                   'SanDiego' : 'Chargers',
                   'St.Louis' : 'Rams',
                   'LARams' : 'Rams',
                   'LAChargers' : 'Chargers',
                   'LasVegas' : 'Raiders',
                   'LosAngeles' : 'Rams', 
                   'NewYork' : 'Giants',
                   'KCChiefs' : 'Chiefs',
                   'Kansas' : 'Chiefs',
                   'Tampa' : 'Buccaneers' 
                        }})

When I run this code, as seen in the picture, the code does not work - Image

As seen, I ran the function on a bunch of different dataframes, but when I sample one of them, there are no changes to the 'Team' column.

I know my code is correct because when I run the code outside of the function, such as below:

nfl_07 = nfl_07.replace({'Team':
                  {'NewEngland' : 'Patriots',
                   'GreenBay' : 'Packers',
                   'Pittsburgh' : 'Steelers'
                                              etc.

This code works for some reason; my nfl_07 dataframe has the correct Team names... Is there something wrong with my function?

1
  • In your function you fail to return the renamed DataFrame, when you call the function you fail to assign the output. So nothing happens.
    – mozway
    Oct 7, 2022 at 4:27

1 Answer 1

1

replace does not act in place by default.

In your function you fail to return the renamed DataFrame, when you call the function you fail to assign the output. So nothing happens.

The assignment to df within the function is local. It does not impact the outer scope.

Either modify in place:

def rename(df):
    df.replace({'Team':
               {'NewEngland' : 'Patriots',
                'GreenBay' : 'Packers',
                # ...
                }}, inplace=True)

rename(nfl_07)

Or return and reassign the output:

def rename(df):
    return df.replace({'Team':
                      {'NewEngland' : 'Patriots',
                       'GreenBay' : 'Packers',
                       # ...
                       }})

nfl_07 = rename(nfl_07)
                   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.