Update Column A where Column B has Value C in Pandas DataFrame












0















Good morning,



I'm trying to update a DataFrame based on the contents of two columns and am running into issues.



Specifically, I have a column called IP, another called VISITTIME. I've added two columns called OLDEST and NEWEST which need to contain the min and max VISITTIME for the IP of that row.



Using:



df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(), 
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")


I can get the min and max times for each IP in the table. I can then iterate over that but I don't know how to update the original DataFrame.



Essentially what I'm asking is how do I do the following in pandas:



UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP; 


I feel like this should be easy and I'm ashamed that it isn't.



Thank you










share|improve this question

























  • Please add an example of input data and expected output.

    – user3471881
    Nov 21 '18 at 18:15











  • See: How to make good reproducible pandas examples

    – root
    Nov 21 '18 at 18:17













  • look at pd.update()

    – Chris
    Nov 21 '18 at 18:40
















0















Good morning,



I'm trying to update a DataFrame based on the contents of two columns and am running into issues.



Specifically, I have a column called IP, another called VISITTIME. I've added two columns called OLDEST and NEWEST which need to contain the min and max VISITTIME for the IP of that row.



Using:



df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(), 
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")


I can get the min and max times for each IP in the table. I can then iterate over that but I don't know how to update the original DataFrame.



Essentially what I'm asking is how do I do the following in pandas:



UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP; 


I feel like this should be easy and I'm ashamed that it isn't.



Thank you










share|improve this question

























  • Please add an example of input data and expected output.

    – user3471881
    Nov 21 '18 at 18:15











  • See: How to make good reproducible pandas examples

    – root
    Nov 21 '18 at 18:17













  • look at pd.update()

    – Chris
    Nov 21 '18 at 18:40














0












0








0








Good morning,



I'm trying to update a DataFrame based on the contents of two columns and am running into issues.



Specifically, I have a column called IP, another called VISITTIME. I've added two columns called OLDEST and NEWEST which need to contain the min and max VISITTIME for the IP of that row.



Using:



df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(), 
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")


I can get the min and max times for each IP in the table. I can then iterate over that but I don't know how to update the original DataFrame.



Essentially what I'm asking is how do I do the following in pandas:



UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP; 


I feel like this should be easy and I'm ashamed that it isn't.



Thank you










share|improve this question
















Good morning,



I'm trying to update a DataFrame based on the contents of two columns and am running into issues.



Specifically, I have a column called IP, another called VISITTIME. I've added two columns called OLDEST and NEWEST which need to contain the min and max VISITTIME for the IP of that row.



Using:



df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(), 
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")


I can get the min and max times for each IP in the table. I can then iterate over that but I don't know how to update the original DataFrame.



Essentially what I'm asking is how do I do the following in pandas:



UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP; 


I feel like this should be easy and I'm ashamed that it isn't.



Thank you







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:43









user3471881

1,1872619




1,1872619










asked Nov 21 '18 at 18:07









DClarkeDClarke

12




12













  • Please add an example of input data and expected output.

    – user3471881
    Nov 21 '18 at 18:15











  • See: How to make good reproducible pandas examples

    – root
    Nov 21 '18 at 18:17













  • look at pd.update()

    – Chris
    Nov 21 '18 at 18:40



















  • Please add an example of input data and expected output.

    – user3471881
    Nov 21 '18 at 18:15











  • See: How to make good reproducible pandas examples

    – root
    Nov 21 '18 at 18:17













  • look at pd.update()

    – Chris
    Nov 21 '18 at 18:40

















Please add an example of input data and expected output.

– user3471881
Nov 21 '18 at 18:15





Please add an example of input data and expected output.

– user3471881
Nov 21 '18 at 18:15













See: How to make good reproducible pandas examples

– root
Nov 21 '18 at 18:17







See: How to make good reproducible pandas examples

– root
Nov 21 '18 at 18:17















look at pd.update()

– Chris
Nov 21 '18 at 18:40





look at pd.update()

– Chris
Nov 21 '18 at 18:40












1 Answer
1






active

oldest

votes


















0














Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.



That should provide some examples, but a few warnings:




  • You need to make sure that both dataframes are using IP as an index,
    or this method will not work.

  • The column (Series) names should be the same, so it will not require any location or column referencing.

  • You may choose to only overwrite NA values, but not applicable for what you are describing here.




DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)


Modify in place using non-NA values from another DataFrame.



Aligns on indices. There is no return value.






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%2f53418151%2fupdate-column-a-where-column-b-has-value-c-in-pandas-dataframe%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














    Depending on your version of pandas (I know in version > 0.22.0),
    there is a method called Dataframe.update.



    That should provide some examples, but a few warnings:




    • You need to make sure that both dataframes are using IP as an index,
      or this method will not work.

    • The column (Series) names should be the same, so it will not require any location or column referencing.

    • You may choose to only overwrite NA values, but not applicable for what you are describing here.




    DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)


    Modify in place using non-NA values from another DataFrame.



    Aligns on indices. There is no return value.






    share|improve this answer




























      0














      Depending on your version of pandas (I know in version > 0.22.0),
      there is a method called Dataframe.update.



      That should provide some examples, but a few warnings:




      • You need to make sure that both dataframes are using IP as an index,
        or this method will not work.

      • The column (Series) names should be the same, so it will not require any location or column referencing.

      • You may choose to only overwrite NA values, but not applicable for what you are describing here.




      DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)


      Modify in place using non-NA values from another DataFrame.



      Aligns on indices. There is no return value.






      share|improve this answer


























        0












        0








        0







        Depending on your version of pandas (I know in version > 0.22.0),
        there is a method called Dataframe.update.



        That should provide some examples, but a few warnings:




        • You need to make sure that both dataframes are using IP as an index,
          or this method will not work.

        • The column (Series) names should be the same, so it will not require any location or column referencing.

        • You may choose to only overwrite NA values, but not applicable for what you are describing here.




        DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)


        Modify in place using non-NA values from another DataFrame.



        Aligns on indices. There is no return value.






        share|improve this answer













        Depending on your version of pandas (I know in version > 0.22.0),
        there is a method called Dataframe.update.



        That should provide some examples, but a few warnings:




        • You need to make sure that both dataframes are using IP as an index,
          or this method will not work.

        • The column (Series) names should be the same, so it will not require any location or column referencing.

        • You may choose to only overwrite NA values, but not applicable for what you are describing here.




        DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)


        Modify in place using non-NA values from another DataFrame.



        Aligns on indices. There is no return value.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 18:44









        rogersdevoprogersdevop

        5613




        5613
































            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%2f53418151%2fupdate-column-a-where-column-b-has-value-c-in-pandas-dataframe%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith