0

I am trying to understand what the following snippet of the code does, in particular the assign(home=1).rename part:

goal_model_data = pd.concat([epl[['HomeTeam','AwayTeam','HomeGoals']].assign(home=1).rename(
            columns={'HomeTeam':'team', 'AwayTeam':'opponent','HomeGoals':'goals'}),
           epl[['AwayTeam','HomeTeam','AwayGoals']].assign(home=0).rename(
            columns={'AwayTeam':'team', 'HomeTeam':'opponent','AwayGoals':'goals'})])

This is taken from text

That deals with regression modelling in pandas. I don't exclude the possibilities of mistakes in the code snippets they provide. But when I run the code, it seems to function properly. But I don't actually understand the meaning of the above given code and how it functions.

Please help.

5
  • Looks to be creating a binary column, which will have a value of 0 for the first df and 1 for the second Commented May 22, 2024 at 14:05
  • pandas.DataFrame.assign Commented May 22, 2024 at 14:21
  • Ok, but if it's new column named "home", why doesn't the name inside assign come enclosed in quotation marks? I would expect it to be assign("home"=1), not assign(home=1) Commented May 22, 2024 at 14:32
  • 1
    DataFrame.assign(**kwargs), it's like doing dict(home=1) (equivalent to {"home": 1}). Commented May 22, 2024 at 15:35
  • 1
    So, df.assign(home=1) is equivalent to df.assign(**{"home": 1}). Commented May 22, 2024 at 15:37

1 Answer 1

0

That uses pd.df.assign() to add a new column with specific values. In the case of assign(home=1), it adds a column called 'home' where every row will have the value of 1.

Sign up to request clarification or add additional context in comments.

2 Comments

Why isn't "home" inside assign() in quotation marks?
I'm not sure why they wrote the code that way. Please see the examples at pandas.pydata.org/docs/reference/api/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.