Insert value into a dataframe column based on condition












0














How do I rewrite the last two lines of the below code, so that the last line does not overwrite the second to last line?



Desired result is that the "color" column will have either "pink" or "orange" values put in depending on which condition is met: "KOM" or "Top 10".



import pandas as pd
import numpy as np

def contains_BO(seg_effs):
# check if segment efforts for activity contain any best overall effort
for eff in seg_effs:
rank = eff['kom_rank']
if rank != None:
if rank == 1:
return "KOM"
else:
return "Top 10"

activities = pd.read_pickle('strava.pk1')
activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'KOM', "orange", "grey")
activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'Top 10', "pink", "grey")









share|improve this question



























    0














    How do I rewrite the last two lines of the below code, so that the last line does not overwrite the second to last line?



    Desired result is that the "color" column will have either "pink" or "orange" values put in depending on which condition is met: "KOM" or "Top 10".



    import pandas as pd
    import numpy as np

    def contains_BO(seg_effs):
    # check if segment efforts for activity contain any best overall effort
    for eff in seg_effs:
    rank = eff['kom_rank']
    if rank != None:
    if rank == 1:
    return "KOM"
    else:
    return "Top 10"

    activities = pd.read_pickle('strava.pk1')
    activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'KOM', "orange", "grey")
    activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'Top 10', "pink", "grey")









    share|improve this question

























      0












      0








      0


      1





      How do I rewrite the last two lines of the below code, so that the last line does not overwrite the second to last line?



      Desired result is that the "color" column will have either "pink" or "orange" values put in depending on which condition is met: "KOM" or "Top 10".



      import pandas as pd
      import numpy as np

      def contains_BO(seg_effs):
      # check if segment efforts for activity contain any best overall effort
      for eff in seg_effs:
      rank = eff['kom_rank']
      if rank != None:
      if rank == 1:
      return "KOM"
      else:
      return "Top 10"

      activities = pd.read_pickle('strava.pk1')
      activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'KOM', "orange", "grey")
      activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'Top 10', "pink", "grey")









      share|improve this question













      How do I rewrite the last two lines of the below code, so that the last line does not overwrite the second to last line?



      Desired result is that the "color" column will have either "pink" or "orange" values put in depending on which condition is met: "KOM" or "Top 10".



      import pandas as pd
      import numpy as np

      def contains_BO(seg_effs):
      # check if segment efforts for activity contain any best overall effort
      for eff in seg_effs:
      rank = eff['kom_rank']
      if rank != None:
      if rank == 1:
      return "KOM"
      else:
      return "Top 10"

      activities = pd.read_pickle('strava.pk1')
      activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'KOM', "orange", "grey")
      activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'Top 10', "pink", "grey")






      python pandas numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 21:32









      barciewicz

      515312




      515312
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You use something like this:



          import pandas as pd

          df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
          df

          a b
          0 0 x
          1 1 x
          2 2 y
          3 3 y

          # assign 5 to rows of "a" where "b" == "x"
          df.loc[df["b"] == "x", "a"] = 5
          df

          a b
          0 5 x
          1 5 x
          2 2 y
          3 3 y


          Alternatively you can create a new column out of a dict of values:



          df["val"] = df["b"].map({"x": 5, "y": 6})

          df

          a b val
          0 5 x 5
          1 5 x 5
          2 2 y 6
          3 3 y 6


          map also supports functions if you need more complex logic.






          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%2f53253433%2finsert-value-into-a-dataframe-column-based-on-condition%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














            You use something like this:



            import pandas as pd

            df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
            df

            a b
            0 0 x
            1 1 x
            2 2 y
            3 3 y

            # assign 5 to rows of "a" where "b" == "x"
            df.loc[df["b"] == "x", "a"] = 5
            df

            a b
            0 5 x
            1 5 x
            2 2 y
            3 3 y


            Alternatively you can create a new column out of a dict of values:



            df["val"] = df["b"].map({"x": 5, "y": 6})

            df

            a b val
            0 5 x 5
            1 5 x 5
            2 2 y 6
            3 3 y 6


            map also supports functions if you need more complex logic.






            share|improve this answer




























              0














              You use something like this:



              import pandas as pd

              df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
              df

              a b
              0 0 x
              1 1 x
              2 2 y
              3 3 y

              # assign 5 to rows of "a" where "b" == "x"
              df.loc[df["b"] == "x", "a"] = 5
              df

              a b
              0 5 x
              1 5 x
              2 2 y
              3 3 y


              Alternatively you can create a new column out of a dict of values:



              df["val"] = df["b"].map({"x": 5, "y": 6})

              df

              a b val
              0 5 x 5
              1 5 x 5
              2 2 y 6
              3 3 y 6


              map also supports functions if you need more complex logic.






              share|improve this answer


























                0












                0








                0






                You use something like this:



                import pandas as pd

                df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
                df

                a b
                0 0 x
                1 1 x
                2 2 y
                3 3 y

                # assign 5 to rows of "a" where "b" == "x"
                df.loc[df["b"] == "x", "a"] = 5
                df

                a b
                0 5 x
                1 5 x
                2 2 y
                3 3 y


                Alternatively you can create a new column out of a dict of values:



                df["val"] = df["b"].map({"x": 5, "y": 6})

                df

                a b val
                0 5 x 5
                1 5 x 5
                2 2 y 6
                3 3 y 6


                map also supports functions if you need more complex logic.






                share|improve this answer














                You use something like this:



                import pandas as pd

                df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
                df

                a b
                0 0 x
                1 1 x
                2 2 y
                3 3 y

                # assign 5 to rows of "a" where "b" == "x"
                df.loc[df["b"] == "x", "a"] = 5
                df

                a b
                0 5 x
                1 5 x
                2 2 y
                3 3 y


                Alternatively you can create a new column out of a dict of values:



                df["val"] = df["b"].map({"x": 5, "y": 6})

                df

                a b val
                0 5 x 5
                1 5 x 5
                2 2 y 6
                3 3 y 6


                map also supports functions if you need more complex logic.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 22:03

























                answered Nov 11 at 21:53









                pawroman

                935612




                935612






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53253433%2finsert-value-into-a-dataframe-column-based-on-condition%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

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation