Pandas DataFrames: Create new rows with calculations across existing rows 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!Dynamic Expression Evaluation in pandas using pd.eval()Add one row to pandas DataFrameSelecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaN“Large data” work flows using pandasHow do I get the row count of a pandas DataFrame?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Strange behavior of Object.defineProperty() in JavaScript

Can a sorcerer use careful spell on himself?

Can the Flaming Sphere spell be rammed into multiple Tiny creatures that are in the same 5-foot square?

Flash light on something

A letter with no particular backstory

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Did any compiler fully use 80-bit floating point?

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?

What to do with repeated rejections for phd position

What is the meaning of 'breadth' in breadth first search?

Is multiple magic items in one inherently imbalanced?

preposition before coffee

How to report t statistic from R

Karn the great creator - 'card from outside the game' in sealed

How does Belgium enforce obligatory attendance in elections?

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

Most bit efficient text communication method?

How long can equipment go unused before powering up runs the risk of damage?

What are the discoveries that have been possible with the rejection of positivism?

Significance of Cersei's obsession with elephants?

Semigroups with no morphisms between them

Draw 4 of the same figure in the same tikzpicture

How would a mousetrap for use in space work?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics



Pandas DataFrames: Create new rows with calculations across existing rows



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!Dynamic Expression Evaluation in pandas using pd.eval()Add one row to pandas DataFrameSelecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaN“Large data” work flows using pandasHow do I get the row count of a pandas DataFrame?How to iterate over rows in a DataFrame in Pandas?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;








6















How can I create new rows from an existing DataFrame by grouping by certain fields (in the example "Country" and "Industry") and applying some math to another field (in the example "Field" and "Value")?



Source DataFrame



df = pd.DataFrame('Country': ['USA','USA','USA','USA','USA','USA','Canada','Canada'],
'Industry': ['Finance', 'Finance', 'Retail',
'Retail', 'Energy', 'Energy',
'Retail', 'Retail'],
'Field': ['Import', 'Export','Import',
'Export','Import', 'Export',
'Import', 'Export'],
'Value': [100, 50, 80, 10, 20, 5, 30, 10])

Country Industry Field Value
0 USA Finance Import 100
1 USA Finance Export 50
2 USA Retail Import 80
3 USA Retail Export 10
4 USA Energy Import 20
5 USA Energy Export 5
6 Canada Retail Import 30
7 Canada Retail Export 10


Target DataFrame



Net = Import - Export



 Country Industry Field Value
0 USA Finance Net 50
1 USA Retail Net 70
2 USA Energy Net 15
3 Canada Retail Net 20









share|improve this question






























    6















    How can I create new rows from an existing DataFrame by grouping by certain fields (in the example "Country" and "Industry") and applying some math to another field (in the example "Field" and "Value")?



    Source DataFrame



    df = pd.DataFrame('Country': ['USA','USA','USA','USA','USA','USA','Canada','Canada'],
    'Industry': ['Finance', 'Finance', 'Retail',
    'Retail', 'Energy', 'Energy',
    'Retail', 'Retail'],
    'Field': ['Import', 'Export','Import',
    'Export','Import', 'Export',
    'Import', 'Export'],
    'Value': [100, 50, 80, 10, 20, 5, 30, 10])

    Country Industry Field Value
    0 USA Finance Import 100
    1 USA Finance Export 50
    2 USA Retail Import 80
    3 USA Retail Export 10
    4 USA Energy Import 20
    5 USA Energy Export 5
    6 Canada Retail Import 30
    7 Canada Retail Export 10


    Target DataFrame



    Net = Import - Export



     Country Industry Field Value
    0 USA Finance Net 50
    1 USA Retail Net 70
    2 USA Energy Net 15
    3 Canada Retail Net 20









    share|improve this question


























      6












      6








      6


      1






      How can I create new rows from an existing DataFrame by grouping by certain fields (in the example "Country" and "Industry") and applying some math to another field (in the example "Field" and "Value")?



      Source DataFrame



      df = pd.DataFrame('Country': ['USA','USA','USA','USA','USA','USA','Canada','Canada'],
      'Industry': ['Finance', 'Finance', 'Retail',
      'Retail', 'Energy', 'Energy',
      'Retail', 'Retail'],
      'Field': ['Import', 'Export','Import',
      'Export','Import', 'Export',
      'Import', 'Export'],
      'Value': [100, 50, 80, 10, 20, 5, 30, 10])

      Country Industry Field Value
      0 USA Finance Import 100
      1 USA Finance Export 50
      2 USA Retail Import 80
      3 USA Retail Export 10
      4 USA Energy Import 20
      5 USA Energy Export 5
      6 Canada Retail Import 30
      7 Canada Retail Export 10


      Target DataFrame



      Net = Import - Export



       Country Industry Field Value
      0 USA Finance Net 50
      1 USA Retail Net 70
      2 USA Energy Net 15
      3 Canada Retail Net 20









      share|improve this question
















      How can I create new rows from an existing DataFrame by grouping by certain fields (in the example "Country" and "Industry") and applying some math to another field (in the example "Field" and "Value")?



      Source DataFrame



      df = pd.DataFrame('Country': ['USA','USA','USA','USA','USA','USA','Canada','Canada'],
      'Industry': ['Finance', 'Finance', 'Retail',
      'Retail', 'Energy', 'Energy',
      'Retail', 'Retail'],
      'Field': ['Import', 'Export','Import',
      'Export','Import', 'Export',
      'Import', 'Export'],
      'Value': [100, 50, 80, 10, 20, 5, 30, 10])

      Country Industry Field Value
      0 USA Finance Import 100
      1 USA Finance Export 50
      2 USA Retail Import 80
      3 USA Retail Export 10
      4 USA Energy Import 20
      5 USA Energy Export 5
      6 Canada Retail Import 30
      7 Canada Retail Export 10


      Target DataFrame



      Net = Import - Export



       Country Industry Field Value
      0 USA Finance Net 50
      1 USA Retail Net 70
      2 USA Energy Net 15
      3 Canada Retail Net 20






      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 at 22:45









      Scott Boston

      58.9k73258




      58.9k73258










      asked Apr 13 at 21:53









      LorenzLorenz

      746




      746






















          5 Answers
          5






          active

          oldest

          votes


















          8














          There are quite possibly many ways. Here's one using groupby and unstack:



          (df.groupby(['Country', 'Industry', 'Field'], sort=False)['Value']
          .sum()
          .unstack('Field')
          .eval('Import - Export')
          .reset_index(name='Value'))

          Country Industry Value
          0 USA Finance 50
          1 USA Retail 70
          2 USA Energy 15
          3 Canada Retail 20





          share|improve this answer




















          • 1





            By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

            – BallpointBen
            Apr 13 at 22:17






          • 1





            @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

            – cs95
            Apr 13 at 22:19











          • Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

            – Lorenz
            Apr 14 at 1:40











          • @Lorenz Oops... fixed, thanks!

            – cs95
            Apr 14 at 1:41











          • @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

            – BallpointBen
            Apr 14 at 3:03


















          4














          IIUC



          df=df.set_index(['Country','Industry'])

          Newdf=(df.loc[df.Field=='Export','Value']-df.loc[df.Field=='Import','Value']).reset_index().assign(Field='Net')
          Newdf
          Country Industry Value Field
          0 USA Finance -50 Net
          1 USA Retail -70 Net
          2 USA Energy -15 Net
          3 Canada Retail -20 Net



          pivot_table



          df.pivot_table(index=['Country','Industry'],columns='Field',values='Value',aggfunc='sum').
          diff(axis=1).
          dropna(1).
          rename(columns='Import':'Value').
          reset_index()
          Out[112]:
          Field Country Industry Value
          0 Canada Retail 20.0
          1 USA Energy 15.0
          2 USA Finance 50.0
          3 USA Retail 70.0





          share|improve this answer
































            2














            You can use Groupby.diff() and after that recreate the Field column and finally use DataFrame.dropna:



            df['Value'] = df.groupby(['Country', 'Industry'])['Value'].diff().abs()
            df['Field'] = 'Net'
            df.dropna(inplace=True)
            df.reset_index(drop=True, inplace=True)

            print(df)
            Country Industry Field Value
            0 USA Finance Net 50.0
            1 USA Retail Net 70.0
            2 USA Energy Net 15.0
            3 Canada Retail Net 20.0





            share|improve this answer






























              2














              You can do it this way to add those rows to your original dataframe:



              df.set_index(['Country','Industry','Field'])
              .unstack()['Value']
              .eval('Net = Import - Export')
              .stack().rename('Value').reset_index()


              Output:



               Country Industry Field Value
              0 Canada Retail Export 10
              1 Canada Retail Import 30
              2 Canada Retail Net 20
              3 USA Energy Export 5
              4 USA Energy Import 20
              5 USA Energy Net 15
              6 USA Finance Export 50
              7 USA Finance Import 100
              8 USA Finance Net 50
              9 USA Retail Export 10
              10 USA Retail Import 80
              11 USA Retail Net 70





              share|improve this answer























              • Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                – Lorenz
                Apr 14 at 1:41






              • 1





                Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                – Lorenz
                Apr 14 at 3:07


















              2














              This answer takes advantage of the fact that pandas puts the group keys in the multiindex of the resulting dataframe. (If there were only one group key, you could use loc.)



              >>> s = df.groupby(['Country', 'Industry', 'Field'])['Value'].sum()
              >>> s.xs('Import', axis=0, level='Field') - s.xs('Export', axis=0, level='Field')
              Country Industry
              Canada Retail 20
              USA Energy 15
              Finance 50
              Retail 70
              Name: Value, dtype: int64





              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%2f55670192%2fpandas-dataframes-create-new-rows-with-calculations-across-existing-rows%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                8














                There are quite possibly many ways. Here's one using groupby and unstack:



                (df.groupby(['Country', 'Industry', 'Field'], sort=False)['Value']
                .sum()
                .unstack('Field')
                .eval('Import - Export')
                .reset_index(name='Value'))

                Country Industry Value
                0 USA Finance 50
                1 USA Retail 70
                2 USA Energy 15
                3 Canada Retail 20





                share|improve this answer




















                • 1





                  By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                  – BallpointBen
                  Apr 13 at 22:17






                • 1





                  @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                  – cs95
                  Apr 13 at 22:19











                • Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                  – Lorenz
                  Apr 14 at 1:40











                • @Lorenz Oops... fixed, thanks!

                  – cs95
                  Apr 14 at 1:41











                • @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                  – BallpointBen
                  Apr 14 at 3:03















                8














                There are quite possibly many ways. Here's one using groupby and unstack:



                (df.groupby(['Country', 'Industry', 'Field'], sort=False)['Value']
                .sum()
                .unstack('Field')
                .eval('Import - Export')
                .reset_index(name='Value'))

                Country Industry Value
                0 USA Finance 50
                1 USA Retail 70
                2 USA Energy 15
                3 Canada Retail 20





                share|improve this answer




















                • 1





                  By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                  – BallpointBen
                  Apr 13 at 22:17






                • 1





                  @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                  – cs95
                  Apr 13 at 22:19











                • Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                  – Lorenz
                  Apr 14 at 1:40











                • @Lorenz Oops... fixed, thanks!

                  – cs95
                  Apr 14 at 1:41











                • @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                  – BallpointBen
                  Apr 14 at 3:03













                8












                8








                8







                There are quite possibly many ways. Here's one using groupby and unstack:



                (df.groupby(['Country', 'Industry', 'Field'], sort=False)['Value']
                .sum()
                .unstack('Field')
                .eval('Import - Export')
                .reset_index(name='Value'))

                Country Industry Value
                0 USA Finance 50
                1 USA Retail 70
                2 USA Energy 15
                3 Canada Retail 20





                share|improve this answer















                There are quite possibly many ways. Here's one using groupby and unstack:



                (df.groupby(['Country', 'Industry', 'Field'], sort=False)['Value']
                .sum()
                .unstack('Field')
                .eval('Import - Export')
                .reset_index(name='Value'))

                Country Industry Value
                0 USA Finance 50
                1 USA Retail 70
                2 USA Energy 15
                3 Canada Retail 20






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 14 at 1:41

























                answered Apr 13 at 21:56









                cs95cs95

                143k25164249




                143k25164249







                • 1





                  By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                  – BallpointBen
                  Apr 13 at 22:17






                • 1





                  @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                  – cs95
                  Apr 13 at 22:19











                • Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                  – Lorenz
                  Apr 14 at 1:40











                • @Lorenz Oops... fixed, thanks!

                  – cs95
                  Apr 14 at 1:41











                • @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                  – BallpointBen
                  Apr 14 at 3:03












                • 1





                  By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                  – BallpointBen
                  Apr 13 at 22:17






                • 1





                  @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                  – cs95
                  Apr 13 at 22:19











                • Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                  – Lorenz
                  Apr 14 at 1:40











                • @Lorenz Oops... fixed, thanks!

                  – cs95
                  Apr 14 at 1:41











                • @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                  – BallpointBen
                  Apr 14 at 3:03







                1




                1





                By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                – BallpointBen
                Apr 13 at 22:17





                By far the best answer. The unstack followed by eval is a really nice trick — better than a second groupby and get_group I would have done

                – BallpointBen
                Apr 13 at 22:17




                1




                1





                @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                – cs95
                Apr 13 at 22:19





                @BallpointBen eval and query are personal favourites of mine from the API. I've made attempts to popularise their use, but their usage is not completely understood. I have a QnA here, if you are interested.

                – cs95
                Apr 13 at 22:19













                Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                – Lorenz
                Apr 14 at 1:40





                Works like a charm. Thank you very much. Very small comment - there is a closing bracket missing in the last line.

                – Lorenz
                Apr 14 at 1:40













                @Lorenz Oops... fixed, thanks!

                – cs95
                Apr 14 at 1:41





                @Lorenz Oops... fixed, thanks!

                – cs95
                Apr 14 at 1:41













                @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                – BallpointBen
                Apr 14 at 3:03





                @coldspeed Actually I think there’s a better way… see my answer. unstack is expensive because it reshapes. Using the structure of the first groupby is more efficient, although it takes two lines.

                – BallpointBen
                Apr 14 at 3:03













                4














                IIUC



                df=df.set_index(['Country','Industry'])

                Newdf=(df.loc[df.Field=='Export','Value']-df.loc[df.Field=='Import','Value']).reset_index().assign(Field='Net')
                Newdf
                Country Industry Value Field
                0 USA Finance -50 Net
                1 USA Retail -70 Net
                2 USA Energy -15 Net
                3 Canada Retail -20 Net



                pivot_table



                df.pivot_table(index=['Country','Industry'],columns='Field',values='Value',aggfunc='sum').
                diff(axis=1).
                dropna(1).
                rename(columns='Import':'Value').
                reset_index()
                Out[112]:
                Field Country Industry Value
                0 Canada Retail 20.0
                1 USA Energy 15.0
                2 USA Finance 50.0
                3 USA Retail 70.0





                share|improve this answer





























                  4














                  IIUC



                  df=df.set_index(['Country','Industry'])

                  Newdf=(df.loc[df.Field=='Export','Value']-df.loc[df.Field=='Import','Value']).reset_index().assign(Field='Net')
                  Newdf
                  Country Industry Value Field
                  0 USA Finance -50 Net
                  1 USA Retail -70 Net
                  2 USA Energy -15 Net
                  3 Canada Retail -20 Net



                  pivot_table



                  df.pivot_table(index=['Country','Industry'],columns='Field',values='Value',aggfunc='sum').
                  diff(axis=1).
                  dropna(1).
                  rename(columns='Import':'Value').
                  reset_index()
                  Out[112]:
                  Field Country Industry Value
                  0 Canada Retail 20.0
                  1 USA Energy 15.0
                  2 USA Finance 50.0
                  3 USA Retail 70.0





                  share|improve this answer



























                    4












                    4








                    4







                    IIUC



                    df=df.set_index(['Country','Industry'])

                    Newdf=(df.loc[df.Field=='Export','Value']-df.loc[df.Field=='Import','Value']).reset_index().assign(Field='Net')
                    Newdf
                    Country Industry Value Field
                    0 USA Finance -50 Net
                    1 USA Retail -70 Net
                    2 USA Energy -15 Net
                    3 Canada Retail -20 Net



                    pivot_table



                    df.pivot_table(index=['Country','Industry'],columns='Field',values='Value',aggfunc='sum').
                    diff(axis=1).
                    dropna(1).
                    rename(columns='Import':'Value').
                    reset_index()
                    Out[112]:
                    Field Country Industry Value
                    0 Canada Retail 20.0
                    1 USA Energy 15.0
                    2 USA Finance 50.0
                    3 USA Retail 70.0





                    share|improve this answer















                    IIUC



                    df=df.set_index(['Country','Industry'])

                    Newdf=(df.loc[df.Field=='Export','Value']-df.loc[df.Field=='Import','Value']).reset_index().assign(Field='Net')
                    Newdf
                    Country Industry Value Field
                    0 USA Finance -50 Net
                    1 USA Retail -70 Net
                    2 USA Energy -15 Net
                    3 Canada Retail -20 Net



                    pivot_table



                    df.pivot_table(index=['Country','Industry'],columns='Field',values='Value',aggfunc='sum').
                    diff(axis=1).
                    dropna(1).
                    rename(columns='Import':'Value').
                    reset_index()
                    Out[112]:
                    Field Country Industry Value
                    0 Canada Retail 20.0
                    1 USA Energy 15.0
                    2 USA Finance 50.0
                    3 USA Retail 70.0






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 13 at 23:12

























                    answered Apr 13 at 21:58









                    Wen-BenWen-Ben

                    127k83872




                    127k83872





















                        2














                        You can use Groupby.diff() and after that recreate the Field column and finally use DataFrame.dropna:



                        df['Value'] = df.groupby(['Country', 'Industry'])['Value'].diff().abs()
                        df['Field'] = 'Net'
                        df.dropna(inplace=True)
                        df.reset_index(drop=True, inplace=True)

                        print(df)
                        Country Industry Field Value
                        0 USA Finance Net 50.0
                        1 USA Retail Net 70.0
                        2 USA Energy Net 15.0
                        3 Canada Retail Net 20.0





                        share|improve this answer



























                          2














                          You can use Groupby.diff() and after that recreate the Field column and finally use DataFrame.dropna:



                          df['Value'] = df.groupby(['Country', 'Industry'])['Value'].diff().abs()
                          df['Field'] = 'Net'
                          df.dropna(inplace=True)
                          df.reset_index(drop=True, inplace=True)

                          print(df)
                          Country Industry Field Value
                          0 USA Finance Net 50.0
                          1 USA Retail Net 70.0
                          2 USA Energy Net 15.0
                          3 Canada Retail Net 20.0





                          share|improve this answer

























                            2












                            2








                            2







                            You can use Groupby.diff() and after that recreate the Field column and finally use DataFrame.dropna:



                            df['Value'] = df.groupby(['Country', 'Industry'])['Value'].diff().abs()
                            df['Field'] = 'Net'
                            df.dropna(inplace=True)
                            df.reset_index(drop=True, inplace=True)

                            print(df)
                            Country Industry Field Value
                            0 USA Finance Net 50.0
                            1 USA Retail Net 70.0
                            2 USA Energy Net 15.0
                            3 Canada Retail Net 20.0





                            share|improve this answer













                            You can use Groupby.diff() and after that recreate the Field column and finally use DataFrame.dropna:



                            df['Value'] = df.groupby(['Country', 'Industry'])['Value'].diff().abs()
                            df['Field'] = 'Net'
                            df.dropna(inplace=True)
                            df.reset_index(drop=True, inplace=True)

                            print(df)
                            Country Industry Field Value
                            0 USA Finance Net 50.0
                            1 USA Retail Net 70.0
                            2 USA Energy Net 15.0
                            3 Canada Retail Net 20.0






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 13 at 22:05









                            ErfanErfan

                            3,6961419




                            3,6961419





















                                2














                                You can do it this way to add those rows to your original dataframe:



                                df.set_index(['Country','Industry','Field'])
                                .unstack()['Value']
                                .eval('Net = Import - Export')
                                .stack().rename('Value').reset_index()


                                Output:



                                 Country Industry Field Value
                                0 Canada Retail Export 10
                                1 Canada Retail Import 30
                                2 Canada Retail Net 20
                                3 USA Energy Export 5
                                4 USA Energy Import 20
                                5 USA Energy Net 15
                                6 USA Finance Export 50
                                7 USA Finance Import 100
                                8 USA Finance Net 50
                                9 USA Retail Export 10
                                10 USA Retail Import 80
                                11 USA Retail Net 70





                                share|improve this answer























                                • Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                  – Lorenz
                                  Apr 14 at 1:41






                                • 1





                                  Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                  – Lorenz
                                  Apr 14 at 3:07















                                2














                                You can do it this way to add those rows to your original dataframe:



                                df.set_index(['Country','Industry','Field'])
                                .unstack()['Value']
                                .eval('Net = Import - Export')
                                .stack().rename('Value').reset_index()


                                Output:



                                 Country Industry Field Value
                                0 Canada Retail Export 10
                                1 Canada Retail Import 30
                                2 Canada Retail Net 20
                                3 USA Energy Export 5
                                4 USA Energy Import 20
                                5 USA Energy Net 15
                                6 USA Finance Export 50
                                7 USA Finance Import 100
                                8 USA Finance Net 50
                                9 USA Retail Export 10
                                10 USA Retail Import 80
                                11 USA Retail Net 70





                                share|improve this answer























                                • Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                  – Lorenz
                                  Apr 14 at 1:41






                                • 1





                                  Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                  – Lorenz
                                  Apr 14 at 3:07













                                2












                                2








                                2







                                You can do it this way to add those rows to your original dataframe:



                                df.set_index(['Country','Industry','Field'])
                                .unstack()['Value']
                                .eval('Net = Import - Export')
                                .stack().rename('Value').reset_index()


                                Output:



                                 Country Industry Field Value
                                0 Canada Retail Export 10
                                1 Canada Retail Import 30
                                2 Canada Retail Net 20
                                3 USA Energy Export 5
                                4 USA Energy Import 20
                                5 USA Energy Net 15
                                6 USA Finance Export 50
                                7 USA Finance Import 100
                                8 USA Finance Net 50
                                9 USA Retail Export 10
                                10 USA Retail Import 80
                                11 USA Retail Net 70





                                share|improve this answer













                                You can do it this way to add those rows to your original dataframe:



                                df.set_index(['Country','Industry','Field'])
                                .unstack()['Value']
                                .eval('Net = Import - Export')
                                .stack().rename('Value').reset_index()


                                Output:



                                 Country Industry Field Value
                                0 Canada Retail Export 10
                                1 Canada Retail Import 30
                                2 Canada Retail Net 20
                                3 USA Energy Export 5
                                4 USA Energy Import 20
                                5 USA Energy Net 15
                                6 USA Finance Export 50
                                7 USA Finance Import 100
                                8 USA Finance Net 50
                                9 USA Retail Export 10
                                10 USA Retail Import 80
                                11 USA Retail Net 70






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Apr 13 at 22:20









                                Scott BostonScott Boston

                                58.9k73258




                                58.9k73258












                                • Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                  – Lorenz
                                  Apr 14 at 1:41






                                • 1





                                  Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                  – Lorenz
                                  Apr 14 at 3:07

















                                • Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                  – Lorenz
                                  Apr 14 at 1:41






                                • 1





                                  Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                  – Lorenz
                                  Apr 14 at 3:07
















                                Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                – Lorenz
                                Apr 14 at 1:41





                                Thanks - actually, I wanted to append it to the original df. So, nice trick to do this all in one command,

                                – Lorenz
                                Apr 14 at 1:41




                                1




                                1





                                Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                – Lorenz
                                Apr 14 at 3:07





                                Coldspeed‘s answer was a slight better fit to my overall code. Took from your code how you appended the result to the original df. Very tight result, though. Pitty that i can not accept two answers. But thanks again!

                                – Lorenz
                                Apr 14 at 3:07











                                2














                                This answer takes advantage of the fact that pandas puts the group keys in the multiindex of the resulting dataframe. (If there were only one group key, you could use loc.)



                                >>> s = df.groupby(['Country', 'Industry', 'Field'])['Value'].sum()
                                >>> s.xs('Import', axis=0, level='Field') - s.xs('Export', axis=0, level='Field')
                                Country Industry
                                Canada Retail 20
                                USA Energy 15
                                Finance 50
                                Retail 70
                                Name: Value, dtype: int64





                                share|improve this answer



























                                  2














                                  This answer takes advantage of the fact that pandas puts the group keys in the multiindex of the resulting dataframe. (If there were only one group key, you could use loc.)



                                  >>> s = df.groupby(['Country', 'Industry', 'Field'])['Value'].sum()
                                  >>> s.xs('Import', axis=0, level='Field') - s.xs('Export', axis=0, level='Field')
                                  Country Industry
                                  Canada Retail 20
                                  USA Energy 15
                                  Finance 50
                                  Retail 70
                                  Name: Value, dtype: int64





                                  share|improve this answer

























                                    2












                                    2








                                    2







                                    This answer takes advantage of the fact that pandas puts the group keys in the multiindex of the resulting dataframe. (If there were only one group key, you could use loc.)



                                    >>> s = df.groupby(['Country', 'Industry', 'Field'])['Value'].sum()
                                    >>> s.xs('Import', axis=0, level='Field') - s.xs('Export', axis=0, level='Field')
                                    Country Industry
                                    Canada Retail 20
                                    USA Energy 15
                                    Finance 50
                                    Retail 70
                                    Name: Value, dtype: int64





                                    share|improve this answer













                                    This answer takes advantage of the fact that pandas puts the group keys in the multiindex of the resulting dataframe. (If there were only one group key, you could use loc.)



                                    >>> s = df.groupby(['Country', 'Industry', 'Field'])['Value'].sum()
                                    >>> s.xs('Import', axis=0, level='Field') - s.xs('Export', axis=0, level='Field')
                                    Country Industry
                                    Canada Retail 20
                                    USA Energy 15
                                    Finance 50
                                    Retail 70
                                    Name: Value, dtype: int64






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Apr 14 at 3:07









                                    BallpointBenBallpointBen

                                    3,7681639




                                    3,7681639



























                                        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%2f55670192%2fpandas-dataframes-create-new-rows-with-calculations-across-existing-rows%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

                                        Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                                        Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                                        What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company