Looking for adding a column to DATAFRAME including filtration of DATAFRAME











up vote
0
down vote

favorite












If I have a Dataframe is like this:



df =



       T1                        price 


0 Today is Tuesday 60



1 Tomorrow is Wednesday 70



2 After Tomorrow is Thursday 80



3 The last day is Friday 90



If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"



This is an example to the Dataframe which I am looking for:



df =



       T1                        price         New 


0 Today is Tuesday 60 Unknow



1 Tomorrow is Wednesday 70 Unknow



2 After Tomorrow is Thursday 80 80



3 The last day is Friday 90 90



I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:



df =



       T1                         price


2 After Tomorrow is Thursday 80



3 The last day is Friday 90



I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.










share|improve this question


























    up vote
    0
    down vote

    favorite












    If I have a Dataframe is like this:



    df =



           T1                        price 


    0 Today is Tuesday 60



    1 Tomorrow is Wednesday 70



    2 After Tomorrow is Thursday 80



    3 The last day is Friday 90



    If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"



    This is an example to the Dataframe which I am looking for:



    df =



           T1                        price         New 


    0 Today is Tuesday 60 Unknow



    1 Tomorrow is Wednesday 70 Unknow



    2 After Tomorrow is Thursday 80 80



    3 The last day is Friday 90 90



    I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:



    df =



           T1                         price


    2 After Tomorrow is Thursday 80



    3 The last day is Friday 90



    I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      If I have a Dataframe is like this:



      df =



             T1                        price 


      0 Today is Tuesday 60



      1 Tomorrow is Wednesday 70



      2 After Tomorrow is Thursday 80



      3 The last day is Friday 90



      If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"



      This is an example to the Dataframe which I am looking for:



      df =



             T1                        price         New 


      0 Today is Tuesday 60 Unknow



      1 Tomorrow is Wednesday 70 Unknow



      2 After Tomorrow is Thursday 80 80



      3 The last day is Friday 90 90



      I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:



      df =



             T1                         price


      2 After Tomorrow is Thursday 80



      3 The last day is Friday 90



      I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.










      share|improve this question













      If I have a Dataframe is like this:



      df =



             T1                        price 


      0 Today is Tuesday 60



      1 Tomorrow is Wednesday 70



      2 After Tomorrow is Thursday 80



      3 The last day is Friday 90



      If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"



      This is an example to the Dataframe which I am looking for:



      df =



             T1                        price         New 


      0 Today is Tuesday 60 Unknow



      1 Tomorrow is Wednesday 70 Unknow



      2 After Tomorrow is Thursday 80 80



      3 The last day is Friday 90 90



      I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:



      df =



             T1                         price


      2 After Tomorrow is Thursday 80



      3 The last day is Friday 90



      I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.







      python dataframe max






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 at 23:44









      Elwakdy

      86




      86
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')



          It is hard to read from your unformatted tables but I believe this is what you want.






          share|improve this answer





















          • Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
            – Elwakdy
            2 days ago










          • I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
            – Elwakdy
            2 days ago










          • Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
            – BernardL
            2 days ago










          • Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
            – Elwakdy
            2 days ago










          • Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
            – BernardL
            2 days ago











          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',
          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%2f53346798%2flooking-for-adding-a-column-to-dataframe-including-filtration-of-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








          up vote
          0
          down vote



          accepted










          Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')



          It is hard to read from your unformatted tables but I believe this is what you want.






          share|improve this answer





















          • Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
            – Elwakdy
            2 days ago










          • I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
            – Elwakdy
            2 days ago










          • Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
            – BernardL
            2 days ago










          • Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
            – Elwakdy
            2 days ago










          • Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
            – BernardL
            2 days ago















          up vote
          0
          down vote



          accepted










          Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')



          It is hard to read from your unformatted tables but I believe this is what you want.






          share|improve this answer





















          • Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
            – Elwakdy
            2 days ago










          • I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
            – Elwakdy
            2 days ago










          • Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
            – BernardL
            2 days ago










          • Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
            – Elwakdy
            2 days ago










          • Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
            – BernardL
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')



          It is hard to read from your unformatted tables but I believe this is what you want.






          share|improve this answer












          Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')



          It is hard to read from your unformatted tables but I believe this is what you want.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 at 0:09









          BernardL

          1,567828




          1,567828












          • Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
            – Elwakdy
            2 days ago










          • I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
            – Elwakdy
            2 days ago










          • Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
            – BernardL
            2 days ago










          • Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
            – Elwakdy
            2 days ago










          • Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
            – BernardL
            2 days ago


















          • Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
            – Elwakdy
            2 days ago










          • I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
            – Elwakdy
            2 days ago










          • Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
            – BernardL
            2 days ago










          • Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
            – Elwakdy
            2 days ago










          • Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
            – BernardL
            2 days ago
















          Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
          – Elwakdy
          2 days ago




          Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
          – Elwakdy
          2 days ago












          I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
          – Elwakdy
          2 days ago




          I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
          – Elwakdy
          2 days ago












          Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
          – BernardL
          2 days ago




          Or you can do this: df['price'].apply(lambda x: 1 if x >= 80 else 0), you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
          – BernardL
          2 days ago












          Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
          – Elwakdy
          2 days ago




          Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
          – Elwakdy
          2 days ago












          Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
          – BernardL
          2 days ago




          Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
          – BernardL
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346798%2flooking-for-adding-a-column-to-dataframe-including-filtration-of-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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?