Update value for every row based on either of two previous columns The 2019 Stack Overflow Developer Survey Results Are InHow to drop rows of Pandas DataFrame whose value in certain columns is NaN“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasDeleting DataFrame row in Pandas based on column valuelooping a function in a data framefinding streaks in pandas dataframeCreating a column with mean statistical values in a tennis dataframepandas - use aggregation function on column for last n values of a datetime column in same dataframePandas DataFrame - Add Column Containing Conditional Sum of “previous” RowsPandas: Cumulative count from two columns

What is this 4-propeller plane?

Limit the amount of RAM Mathematica may access?

How can I fix this gap between bookcases I made?

What is the motivation for a law requiring 2 parties to consent for recording a conversation

Should I use my personal or workplace e-mail when registering to external websites for work purpose?

I looked up a future colleague on LinkedIn before I started a job. I told my colleague about it and he seemed surprised. Should I apologize?

Monty Hall variation

Why Did Howard Stark Use All The Vibranium They Had On A Prototype Shield?

Access elements in std::string where positon of string is greater than its size

What does Linus Torvalds means when he says that git "never ever" tracks a file?

Are there any other methods to apply to solving simultaneous equations?

In microwave frequencies, do you use a circulator when you need a (near) perfect diode?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

How are circuits which use complex ICs normally simulated?

Does a dangling wire really electrocute me if I'm standing in water?

Deadlock Graph and Interpretation, solution to avoid

JSON.serialize: is it possible to suppress null values of a map?

How to deal with fear of taking dependencies

"To split hairs" vs "To be pedantic"

Spanish for "widget"

Could JWST stay at L2 "forever"?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

Extreme, unacceptable situation and I can't attend work tomorrow morning

Where does the "burst of radiance" from Holy Weapon originate?



Update value for every row based on either of two previous columns



The 2019 Stack Overflow Developer Survey Results Are InHow to drop rows of Pandas DataFrame whose value in certain columns is NaN“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasDeleting DataFrame row in Pandas based on column valuelooping a function in a data framefinding streaks in pandas dataframeCreating a column with mean statistical values in a tennis dataframepandas - use aggregation function on column for last n values of a datetime column in same dataframePandas DataFrame - Add Column Containing Conditional Sum of “previous” RowsPandas: Cumulative count from two columns



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and the loser. I have sorted the dataframe on date. Currently I am trying to calculate the ELO-rating of both the winner and the loser for every match (thus every row).
To calculate the ELO-rating, one needs the ELO-rating for both players in their previous match. Another difficulty arises, as the winner of the current match might have been a loser in his previous match. As a result, the 'winner_player_id' value of the current match might be in the 'loser_player_id' column for the previous match.



I am not sure how to efficiently select the previous ELO-ratings for both players per row, as this entails a search across multiple columns.



Every row includes the following columns:



array(['match_id', 'tourney_dates', 'round_order', 'tourney_name',
'tourney_year_id', 'tourney_round_name', 'winner_player_id',
'winner_slug', 'loser_player_id', 'loser_slug', 'elo_player_1', 'elo_player_2'])


Your time is appreciated!










share|improve this question
























  • Refer How to create minimal, complete and verifiable example

    – AkshayNevrekar
    Mar 8 at 8:41











  • Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

    – Nenri
    Mar 8 at 8:49


















-1















I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and the loser. I have sorted the dataframe on date. Currently I am trying to calculate the ELO-rating of both the winner and the loser for every match (thus every row).
To calculate the ELO-rating, one needs the ELO-rating for both players in their previous match. Another difficulty arises, as the winner of the current match might have been a loser in his previous match. As a result, the 'winner_player_id' value of the current match might be in the 'loser_player_id' column for the previous match.



I am not sure how to efficiently select the previous ELO-ratings for both players per row, as this entails a search across multiple columns.



Every row includes the following columns:



array(['match_id', 'tourney_dates', 'round_order', 'tourney_name',
'tourney_year_id', 'tourney_round_name', 'winner_player_id',
'winner_slug', 'loser_player_id', 'loser_slug', 'elo_player_1', 'elo_player_2'])


Your time is appreciated!










share|improve this question
























  • Refer How to create minimal, complete and verifiable example

    – AkshayNevrekar
    Mar 8 at 8:41











  • Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

    – Nenri
    Mar 8 at 8:49














-1












-1








-1








I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and the loser. I have sorted the dataframe on date. Currently I am trying to calculate the ELO-rating of both the winner and the loser for every match (thus every row).
To calculate the ELO-rating, one needs the ELO-rating for both players in their previous match. Another difficulty arises, as the winner of the current match might have been a loser in his previous match. As a result, the 'winner_player_id' value of the current match might be in the 'loser_player_id' column for the previous match.



I am not sure how to efficiently select the previous ELO-ratings for both players per row, as this entails a search across multiple columns.



Every row includes the following columns:



array(['match_id', 'tourney_dates', 'round_order', 'tourney_name',
'tourney_year_id', 'tourney_round_name', 'winner_player_id',
'winner_slug', 'loser_player_id', 'loser_slug', 'elo_player_1', 'elo_player_2'])


Your time is appreciated!










share|improve this question
















I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and the loser. I have sorted the dataframe on date. Currently I am trying to calculate the ELO-rating of both the winner and the loser for every match (thus every row).
To calculate the ELO-rating, one needs the ELO-rating for both players in their previous match. Another difficulty arises, as the winner of the current match might have been a loser in his previous match. As a result, the 'winner_player_id' value of the current match might be in the 'loser_player_id' column for the previous match.



I am not sure how to efficiently select the previous ELO-ratings for both players per row, as this entails a search across multiple columns.



Every row includes the following columns:



array(['match_id', 'tourney_dates', 'round_order', 'tourney_name',
'tourney_year_id', 'tourney_round_name', 'winner_player_id',
'winner_slug', 'loser_player_id', 'loser_slug', 'elo_player_1', 'elo_player_2'])


Your time is appreciated!







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 9:20







Jesse

















asked Mar 8 at 8:39









JesseJesse

11




11












  • Refer How to create minimal, complete and verifiable example

    – AkshayNevrekar
    Mar 8 at 8:41











  • Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

    – Nenri
    Mar 8 at 8:49


















  • Refer How to create minimal, complete and verifiable example

    – AkshayNevrekar
    Mar 8 at 8:41











  • Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

    – Nenri
    Mar 8 at 8:49

















Refer How to create minimal, complete and verifiable example

– AkshayNevrekar
Mar 8 at 8:41





Refer How to create minimal, complete and verifiable example

– AkshayNevrekar
Mar 8 at 8:41













Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

– Nenri
Mar 8 at 8:49






Also, i feel like your issue isn't quite related with the title you gave to it, 'cause it's not a total over different column indexes but a searching of previous values per row. Also i think you can check the apply method from dataframe (pandas.pydata.org/pandas-docs/stable/reference/api/…)

– Nenri
Mar 8 at 8:49













1 Answer
1






active

oldest

votes


















0














One approach would be to sort each winner and loser in each row by player name/ID, so the order will be stable regardless of who wins/loses. Here's an example:



df.join(pd.DataFrame(
np.sort(df[['winner_name', 'loser_name']].values, axis=1),
columns=['name1', 'name2']))

df.head(10)


Output:



 winner_name loser_name name1 name2
0 Nicklas Kulti Michael Stich Michael Stich Nicklas Kulti
1 Michael Stich Jim Courier Jim Courier Michael Stich
2 Nicklas Kulti Magnus Larsson Magnus Larsson Nicklas Kulti
3 Jim Courier Martin Sinner Jim Courier Martin Sinner
4 Michael Stich Jimmy Arias Jimmy Arias Michael Stich
5 Nicklas Kulti Fabrice Santoro Fabrice Santoro Nicklas Kulti
6 Magnus Larsson Patrik Kuhnen Magnus Larsson Patrik Kuhnen
7 Jim Courier Paul Haarhuis Jim Courier Paul Haarhuis
8 Nicklas Kulti Magnus Gustafsson Magnus Gustafsson Nicklas Kulti
9 Michael Stich Gilad Bloom Gilad Bloom Michael Stich





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059474%2fupdate-value-for-every-row-based-on-either-of-two-previous-columns%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    One approach would be to sort each winner and loser in each row by player name/ID, so the order will be stable regardless of who wins/loses. Here's an example:



    df.join(pd.DataFrame(
    np.sort(df[['winner_name', 'loser_name']].values, axis=1),
    columns=['name1', 'name2']))

    df.head(10)


    Output:



     winner_name loser_name name1 name2
    0 Nicklas Kulti Michael Stich Michael Stich Nicklas Kulti
    1 Michael Stich Jim Courier Jim Courier Michael Stich
    2 Nicklas Kulti Magnus Larsson Magnus Larsson Nicklas Kulti
    3 Jim Courier Martin Sinner Jim Courier Martin Sinner
    4 Michael Stich Jimmy Arias Jimmy Arias Michael Stich
    5 Nicklas Kulti Fabrice Santoro Fabrice Santoro Nicklas Kulti
    6 Magnus Larsson Patrik Kuhnen Magnus Larsson Patrik Kuhnen
    7 Jim Courier Paul Haarhuis Jim Courier Paul Haarhuis
    8 Nicklas Kulti Magnus Gustafsson Magnus Gustafsson Nicklas Kulti
    9 Michael Stich Gilad Bloom Gilad Bloom Michael Stich





    share|improve this answer





























      0














      One approach would be to sort each winner and loser in each row by player name/ID, so the order will be stable regardless of who wins/loses. Here's an example:



      df.join(pd.DataFrame(
      np.sort(df[['winner_name', 'loser_name']].values, axis=1),
      columns=['name1', 'name2']))

      df.head(10)


      Output:



       winner_name loser_name name1 name2
      0 Nicklas Kulti Michael Stich Michael Stich Nicklas Kulti
      1 Michael Stich Jim Courier Jim Courier Michael Stich
      2 Nicklas Kulti Magnus Larsson Magnus Larsson Nicklas Kulti
      3 Jim Courier Martin Sinner Jim Courier Martin Sinner
      4 Michael Stich Jimmy Arias Jimmy Arias Michael Stich
      5 Nicklas Kulti Fabrice Santoro Fabrice Santoro Nicklas Kulti
      6 Magnus Larsson Patrik Kuhnen Magnus Larsson Patrik Kuhnen
      7 Jim Courier Paul Haarhuis Jim Courier Paul Haarhuis
      8 Nicklas Kulti Magnus Gustafsson Magnus Gustafsson Nicklas Kulti
      9 Michael Stich Gilad Bloom Gilad Bloom Michael Stich





      share|improve this answer



























        0












        0








        0







        One approach would be to sort each winner and loser in each row by player name/ID, so the order will be stable regardless of who wins/loses. Here's an example:



        df.join(pd.DataFrame(
        np.sort(df[['winner_name', 'loser_name']].values, axis=1),
        columns=['name1', 'name2']))

        df.head(10)


        Output:



         winner_name loser_name name1 name2
        0 Nicklas Kulti Michael Stich Michael Stich Nicklas Kulti
        1 Michael Stich Jim Courier Jim Courier Michael Stich
        2 Nicklas Kulti Magnus Larsson Magnus Larsson Nicklas Kulti
        3 Jim Courier Martin Sinner Jim Courier Martin Sinner
        4 Michael Stich Jimmy Arias Jimmy Arias Michael Stich
        5 Nicklas Kulti Fabrice Santoro Fabrice Santoro Nicklas Kulti
        6 Magnus Larsson Patrik Kuhnen Magnus Larsson Patrik Kuhnen
        7 Jim Courier Paul Haarhuis Jim Courier Paul Haarhuis
        8 Nicklas Kulti Magnus Gustafsson Magnus Gustafsson Nicklas Kulti
        9 Michael Stich Gilad Bloom Gilad Bloom Michael Stich





        share|improve this answer















        One approach would be to sort each winner and loser in each row by player name/ID, so the order will be stable regardless of who wins/loses. Here's an example:



        df.join(pd.DataFrame(
        np.sort(df[['winner_name', 'loser_name']].values, axis=1),
        columns=['name1', 'name2']))

        df.head(10)


        Output:



         winner_name loser_name name1 name2
        0 Nicklas Kulti Michael Stich Michael Stich Nicklas Kulti
        1 Michael Stich Jim Courier Jim Courier Michael Stich
        2 Nicklas Kulti Magnus Larsson Magnus Larsson Nicklas Kulti
        3 Jim Courier Martin Sinner Jim Courier Martin Sinner
        4 Michael Stich Jimmy Arias Jimmy Arias Michael Stich
        5 Nicklas Kulti Fabrice Santoro Fabrice Santoro Nicklas Kulti
        6 Magnus Larsson Patrik Kuhnen Magnus Larsson Patrik Kuhnen
        7 Jim Courier Paul Haarhuis Jim Courier Paul Haarhuis
        8 Nicklas Kulti Magnus Gustafsson Magnus Gustafsson Nicklas Kulti
        9 Michael Stich Gilad Bloom Gilad Bloom Michael Stich






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 11:33

























        answered Mar 8 at 10:32









        perlperl

        1,928416




        1,928416





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059474%2fupdate-value-for-every-row-based-on-either-of-two-previous-columns%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved