get value from pandas segment and subtract in place Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?How to get the ASCII value of a character?How to return multiple values from a function?How to subtract a day from a date?Why can't Python parse this JSON data?Use a list of values to select rows from a pandas dataframeDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow do I get the row count of a pandas DataFrame?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Bonus calculation: Am I making a mountain out of a molehill?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

do i need a schengen visa for a direct flight to amsterdam?

What would be the ideal power source for a cybernetic eye?

Diagram with tikz

Why are there no cargo aircraft with "flying wing" design?

What's the difference between `auto x = vector<int>()` and `vector<int> x`?

Does surprise arrest existing movement?

If Jon Snow became King of the Seven Kingdoms what would his regnal number be?

What's the purpose of writing one's academic bio in 3rd person?

Disable hyphenation for an entire paragraph

How does a Death Domain cleric's Touch of Death feature work with Touch-range spells delivered by familiars?

Why aren't air breathing engines used as small first stages

Should I call the interviewer directly, if HR aren't responding?

How to bypass password on Windows XP account?

Is the address of a local variable a constexpr?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?

What do you call a phrase that's not an idiom yet?

Storing hydrofluoric acid before the invention of plastics

Are my PIs rude or am I just being too sensitive?

How to do this path/lattice with tikz

Why is black pepper both grey and black?

I am not a queen, who am I?

What does the "x" in "x86" represent?



get value from pandas segment and subtract in place



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?How to get the ASCII value of a character?How to return multiple values from a function?How to subtract a day from a date?Why can't Python parse this JSON data?Use a list of values to select rows from a pandas dataframeDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow do I get the row count of a pandas DataFrame?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers



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








1















i have a table with values similar to



val1 val2 val3 segVal
0 12.3 88.2
20 0 0
50 14.5 88.7
70 0 0
85 0 0
90 18.2 88.9


for my segVal, i need to use the differences from my val1 columns where val2 is known. so my first segment would be zero to 50, i'm subtracting from 0 and applying that to all segVal rows. my next segment is at 90 so i would subtract that from 50 and apply that.



So my output table would be



val1 val2 val3 segVal
0 12.3 88.2 50
20 0 0 50
50 14.5 88.7 50
70 0 0 40
85 0 0 40
90 18.2 88.9 40


my current somewhat working method is



df1 = df[df.val2 != 0]
df1 = df1.copy()
df1.segVal=(df1['val1'].diff(-1))*1


so i'm creating a additional df and calculating the values this way, then merging back the values with the original df.



It seems there has got to be a better way to do this, I mean, my method works, but doesn't' seem too efficient creating additional df's










share|improve this question






























    1















    i have a table with values similar to



    val1 val2 val3 segVal
    0 12.3 88.2
    20 0 0
    50 14.5 88.7
    70 0 0
    85 0 0
    90 18.2 88.9


    for my segVal, i need to use the differences from my val1 columns where val2 is known. so my first segment would be zero to 50, i'm subtracting from 0 and applying that to all segVal rows. my next segment is at 90 so i would subtract that from 50 and apply that.



    So my output table would be



    val1 val2 val3 segVal
    0 12.3 88.2 50
    20 0 0 50
    50 14.5 88.7 50
    70 0 0 40
    85 0 0 40
    90 18.2 88.9 40


    my current somewhat working method is



    df1 = df[df.val2 != 0]
    df1 = df1.copy()
    df1.segVal=(df1['val1'].diff(-1))*1


    so i'm creating a additional df and calculating the values this way, then merging back the values with the original df.



    It seems there has got to be a better way to do this, I mean, my method works, but doesn't' seem too efficient creating additional df's










    share|improve this question


























      1












      1








      1








      i have a table with values similar to



      val1 val2 val3 segVal
      0 12.3 88.2
      20 0 0
      50 14.5 88.7
      70 0 0
      85 0 0
      90 18.2 88.9


      for my segVal, i need to use the differences from my val1 columns where val2 is known. so my first segment would be zero to 50, i'm subtracting from 0 and applying that to all segVal rows. my next segment is at 90 so i would subtract that from 50 and apply that.



      So my output table would be



      val1 val2 val3 segVal
      0 12.3 88.2 50
      20 0 0 50
      50 14.5 88.7 50
      70 0 0 40
      85 0 0 40
      90 18.2 88.9 40


      my current somewhat working method is



      df1 = df[df.val2 != 0]
      df1 = df1.copy()
      df1.segVal=(df1['val1'].diff(-1))*1


      so i'm creating a additional df and calculating the values this way, then merging back the values with the original df.



      It seems there has got to be a better way to do this, I mean, my method works, but doesn't' seem too efficient creating additional df's










      share|improve this question
















      i have a table with values similar to



      val1 val2 val3 segVal
      0 12.3 88.2
      20 0 0
      50 14.5 88.7
      70 0 0
      85 0 0
      90 18.2 88.9


      for my segVal, i need to use the differences from my val1 columns where val2 is known. so my first segment would be zero to 50, i'm subtracting from 0 and applying that to all segVal rows. my next segment is at 90 so i would subtract that from 50 and apply that.



      So my output table would be



      val1 val2 val3 segVal
      0 12.3 88.2 50
      20 0 0 50
      50 14.5 88.7 50
      70 0 0 40
      85 0 0 40
      90 18.2 88.9 40


      my current somewhat working method is



      df1 = df[df.val2 != 0]
      df1 = df1.copy()
      df1.segVal=(df1['val1'].diff(-1))*1


      so i'm creating a additional df and calculating the values this way, then merging back the values with the original df.



      It seems there has got to be a better way to do this, I mean, my method works, but doesn't' seem too efficient creating additional df's







      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 16:34







      Messak

















      asked Mar 8 at 16:25









      MessakMessak

      1098




      1098






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Here's one way:



          df['segVal'] = df.where(df.val2.ne(0)).val1.dropna().diff().reindex(df.index).bfill()

          val1 val2 val3 segVal
          0 0 12.3 88.2 50.0
          1 20 0.0 0.0 50.0
          2 50 14.5 88.7 50.0
          3 70 0.0 0.0 40.0
          4 85 0.0 0.0 40.0
          5 90 18.2 88.9 40.0





          share|improve this answer

























          • Thank you very much, i knew there was a cleaner and better way.

            – Messak
            Mar 8 at 16:38












          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%2f55067182%2fget-value-from-pandas-segment-and-subtract-in-place%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









          1














          Here's one way:



          df['segVal'] = df.where(df.val2.ne(0)).val1.dropna().diff().reindex(df.index).bfill()

          val1 val2 val3 segVal
          0 0 12.3 88.2 50.0
          1 20 0.0 0.0 50.0
          2 50 14.5 88.7 50.0
          3 70 0.0 0.0 40.0
          4 85 0.0 0.0 40.0
          5 90 18.2 88.9 40.0





          share|improve this answer

























          • Thank you very much, i knew there was a cleaner and better way.

            – Messak
            Mar 8 at 16:38
















          1














          Here's one way:



          df['segVal'] = df.where(df.val2.ne(0)).val1.dropna().diff().reindex(df.index).bfill()

          val1 val2 val3 segVal
          0 0 12.3 88.2 50.0
          1 20 0.0 0.0 50.0
          2 50 14.5 88.7 50.0
          3 70 0.0 0.0 40.0
          4 85 0.0 0.0 40.0
          5 90 18.2 88.9 40.0





          share|improve this answer

























          • Thank you very much, i knew there was a cleaner and better way.

            – Messak
            Mar 8 at 16:38














          1












          1








          1







          Here's one way:



          df['segVal'] = df.where(df.val2.ne(0)).val1.dropna().diff().reindex(df.index).bfill()

          val1 val2 val3 segVal
          0 0 12.3 88.2 50.0
          1 20 0.0 0.0 50.0
          2 50 14.5 88.7 50.0
          3 70 0.0 0.0 40.0
          4 85 0.0 0.0 40.0
          5 90 18.2 88.9 40.0





          share|improve this answer















          Here's one way:



          df['segVal'] = df.where(df.val2.ne(0)).val1.dropna().diff().reindex(df.index).bfill()

          val1 val2 val3 segVal
          0 0 12.3 88.2 50.0
          1 20 0.0 0.0 50.0
          2 50 14.5 88.7 50.0
          3 70 0.0 0.0 40.0
          4 85 0.0 0.0 40.0
          5 90 18.2 88.9 40.0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 16:36

























          answered Mar 8 at 16:31









          ALollzALollz

          16.8k41838




          16.8k41838












          • Thank you very much, i knew there was a cleaner and better way.

            – Messak
            Mar 8 at 16:38


















          • Thank you very much, i knew there was a cleaner and better way.

            – Messak
            Mar 8 at 16:38

















          Thank you very much, i knew there was a cleaner and better way.

          – Messak
          Mar 8 at 16:38






          Thank you very much, i knew there was a cleaner and better way.

          – Messak
          Mar 8 at 16:38




















          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%2f55067182%2fget-value-from-pandas-segment-and-subtract-in-place%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

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко